title: Mongo -Select parent document with maximum child documents count, Faster way?
body: <p>I'm quite new to mongo, and trying to get work following query.and is working fine too, But it's taking a little bit more time. I think I'm doing something wrong.</p>
  
  <p>There are many number of documents in a collection <code>parent</code>, near about 6000. Each document has certain number of <code>childs</code> (childs is an another collection with 40000 documents in it). <code>parents &amp; childs</code> are associated with each other by an attribute in the document called <code>parent_id</code>. Please see the following code. Following code takes approximate 1 minute to execute the queries. I don't think mongo should take that much time. </p>
  
  <pre><code>function getChildMaxDocCount(){
      var maxLen = 0;
      var bigSizeParent =  null;
      db.parents.find().forEach(function (parent){
      var currentcount = db.childs.count({parent_id:parent._id});
      if(currcount &gt; maxLen){
          maxLen = currcount;
          bigSizeParent = parent._id;
      }
      });
  
      printjson({"maxLen":maxLen, "bigSizeParent":bigSizeParent });
  }
  </code></pre>
  
  <p>Is there any feasible/optimal way to achieve this?</p>