Can I apply forEach in aggregate query in MongoDB?

后端 未结 1 1939
后悔当初
后悔当初 2021-01-01 01:28

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 c

相关标签:
1条回答
  • 2021-01-01 02:01

    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);
    });
    
    0 讨论(0)
提交回复
热议问题