title: Robomongo : Exceeded memory limit for $group
body: <p>I`m using a script to remove duplicates on mongo, it worked in a collection with 10 items that I used as a test but when I used for the real collection with 6 million documents, I get an error.</p>
  
  <p>This is the script which I ran in Robomongo (now known as <a href="https://robomongo.org/" rel="noreferrer">Robo 3T</a>):</p>
  
  <pre><code>var bulk = db.getCollection('RAW_COLLECTION').initializeOrderedBulkOp();
  var count = 0;
  
  db.getCollection('RAW_COLLECTION').aggregate([
    // Group on unique value storing _id values to array and count 
    { "$group": {
      "_id": { RegisterNumber: "$RegisterNumber", Region: "$Region" },
      "ids": { "$push": "$_id" },
      "count": { "$sum": 1 }      
    }},
    // Only return things that matched more than once. i.e a duplicate
    { "$match": { "count": { "$gt": 1 } } }
  ]).forEach(function(doc) {
    var keep = doc.ids.shift();     // takes the first _id from the array
  
    bulk.find({ "_id": { "$in": doc.ids }}).remove(); // remove all remaining _id matches
    count++;
  
    if ( count % 500 == 0 ) {  // only actually write per 500 operations
        bulk.execute();
        bulk = db.getCollection('RAW_COLLECTION').initializeOrderedBulkOp();  // re-init after execute
    }
  });
  
  // Clear any queued operations
  if ( count % 500 != 0 )
      bulk.execute();
  </code></pre>
  
  <p>This is the error message:</p>
  
  <pre><code>Error: command failed: {
      "errmsg" : "exception: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.",
      "code" : 16945,
      "ok" : 0
  } : aggregate failed :
  _getErrorWithCode@src/mongo/shell/utils.js:23:13
  doassert@src/mongo/shell/assert.js:13:14
  assert.commandWorked@src/mongo/shell/assert.js:266:5
  DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
  @(shell):1:1
  </code></pre>
  
  <p>So I need to set <code>allowDiskUse:true</code> to work? Where do I do that in the script and is there any problem doing this?</p>