Can I apply forEach in aggregate query in MongoDB?
<p>I have a member collection and find members by specific conditions and after get members I need to do some calculation for each member. To calculate need query on the same collection.</p>

<p>My process is</p>

<pre><code>var eachMemberInfo = [];
var members = db.collection('member').find({ createdDate: currentDate, country: 'BD'}).toArray();
members.forEach(function(doc) {
  var result = db.collection('member').aggregate([
    { $match: { memberType: doc.memberType, country : doc.country } },
    {
      $group: {
        _id: {memberType:"$memberType",country:"$country"},
        memberCount: { $sum: {$cond:[{$gt: ["$numberOfInvitees",0]},1,0]} },
        lessCount: { $sum: {$cond:[{$and:[{$lt:["$numberOfInvitees", doc.numberOfInvitees]}, {$gt: ["$numberOfInvitees",0]}]},1,0]} },
        sameCount: { $sum: {$cond:[{$eq: ["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
      }
    }
  ]).toArray();

   eachMemberInfo.push({memberId:doc.memberId,memberCount: result[0].memberCount, lessCount: result[0].lessCount});

});
</code></pre>

<p><strong>My question is how can I do this using single aggregate query?</strong></p>

<p>can any one help me pls :)</p>

<p><strong>For example:</strong></p>

<p><strong>member</strong> collection like:</p>

<pre><code>[{
    "_id" : ObjectId("57905b2ca644ec06142a8c06"),
    "memberID" : 80,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
},
{
    "_id" : ObjectId("57905b2ca644ec06142a8c09"),
    "memberID" : 81,
    "memberType" : "N",
    "numberOfInvitees" : 3,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8fgh"),
    "memberID" : 82,
    "memberType" : "N",
    "numberOfInvitees" : 4,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 83,
    "memberType" : "N",
    "numberOfInvitees" : 1,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 84,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
..............
]
</code></pre>

<p>Expected result in <strong>eachMemberInfo</strong> like:</p>

<pre><code>[
  { memberID : 80, memberCount:5,lessCount: 1,sameCount:2 },
  { memberID : 81, memberCount:5,lessCount: 3,sameCount:1 },
  { memberID : 82, memberCount:5,lessCount: 4,sameCount:1 },
  { memberID : 83, memberCount:5,lessCount: 0,sameCount:1 },
  { memberID : 84, memberCount:5,lessCount: 1,sameCount:2 }
]
</code></pre>