title: How fast is MongoDB's aggregate query over 10 million entries of simple data?
body: <p>Let's say you have a database of People.</p>
  
  <pre><code>[
    #1 {qualities: ['brown', 'nice', 'happy' 'cool' 'cheery']}
    #2 {qualities: ['blue', 'okay', 'happy' 'decent' 'cheery']}
    #3 {qualities: ['green', 'alright', 'happy' 'cool' 'cheery']}
  ]
  </code></pre>
  
  <p>Here's the People <strong>schema and model:</strong></p>
  
  <pre><code>var peopleSchema = mongoose.Schema({
    qualities: [],
  });
  
  var People = mongoose.model('People', peopleSchema);
  </code></pre>
  
  <p>If you want to get data according to maximum match document then we use the <strong>aggregate</strong> query:</p>
  
  <pre><code>People.aggregate([
    {$unwind:"$qualities"},
    {$match:{'qualities': {$in: ["brown", "happy", "cool"]}}},
    {$group:{_id:"$_id",count:{$sum:1}}},
    {$sort:{count:-1}}
  ]).exec(function(err, persons) {
        console.log(persons)
  });
  </code></pre>
  
  <p>it will return <strong>1, 3,2</strong> because for <strong>first one</strong> matched with <em>3</em>
   items, <strong>third one</strong> matched with <em>2</em> items and <strong>second one</strong> matched with <em>1</em> item.</p>
  
  <h2><strong>Question</strong></h2>
  
  <p>This aggregate works fast for my database of 10,000 people - in fact, it completed this in 273.199ms. However, how will it fare for a MongoDB of 10 million entries? If these rates are proportional [100k:2.7s, 1m:27s, 10m:4m30s], it could take 4 minutes and 30 seconds. Perhaps the rate is not proportional, I do not know. But is there any optimization or suggestions for querying such a large database if my time hypothesis happens to be true?</p>