shaishab roy shaishab roy - 4 months ago 60
Node.js Question

Can I apply forEach in aggregate query in MongoDB?

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.

My process is

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});

});


My question is how can I do this using single aggregate query?

can any one help me pls :)

For example:

member collection like:

[{
"_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"
}
..............
]


Expected result in eachMemberInfo like:

[
{ 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 }
]

Answer

You can't do that with the aggregation pipeline. You should understand that MongoDB aggregation is a series of special operators applied to a collection. When you execute an aggregation pipeline, MongoDB pipes operators into each other i.e. the output of an operator becomes the input of the following operator. The result of each operator is a new collection of documents.

Hence what you are trying to achieve in the above can be simply rewritten as the following pipeline without the need to create an array of documents first:

var collection = db.collection('member'), 
    pipeline = [
        { "$match": { createdDate: currentDate, country: 'BD' } },
        {
            "$group": {
                "_id": { "memberType": "$memberType", "country": "$country" },
                "memberCount": { 
                    "$sum": { "$cond":[ { "$gt": ["$numberOfInvitees", 0] }, 1, 0 ] } 
                },
                "sameCount": { "$sum": 1 } 
            }
        }
    ];

collection.aggregate(pipeline, function(err, result){
    if (err) throw err;
    console.log(result);
});

UPDATE

Follow-up to the changes to your question, running the following aggregation pipeline will give you the desired result:

var collection = db.collection('member'), 
    pipeline = [   
        { "$match": { createdDate: currentDate, country: 'BD' } },
        {
            "$group": {
                "_id": { 
                    "memberType": "$memberType", 
                    "country": "$country" 
                },            
                "invitees":{ 
                    "$push":  {
                        "memberID": "$memberID",
                        "count": "$numberOfInvitees"
                    }
                },
                "inviteesList": { "$push": "$numberOfInvitees" },
                "memberCount": { "$sum": 1 } 
            }
        },
        { "$unwind": "$invitees" },
        { "$unwind": "$inviteesList" },
        { 
            "$group": {
                "_id": "$invitees.memberID",
                "sameInviteesCount": { 
                     "$sum": { 
                        "$cond": [ 
                            { "$eq": ["$inviteesList", "$invitees.count"] }, 
                            1, 0 
                        ] 
                    }
                },
                "lessInviteesCount": { 
                    "$sum": { 
                        "$cond":[ 
                            { "$lt": ["$inviteesList", "$invitees.count"] }, 
                            1, 0 
                        ] 
                    }
                },
                "memberCount": { "$first": "$memberCount" }
            }
        }
    ];

collection.aggregate(pipeline, function(err, result){
    if (err) throw err;
    console.log(result);
});