How to group query with multiple $cond?

前端 未结 1 1739
庸人自扰
庸人自扰 2021-02-01 03:40

I want to query like below, but this contains only one $cond.

How to query with two $cond?

collection.aggregate(
    { 
                


        
1条回答
  •  长发绾君心
    2021-02-01 03:44

    You want to use a compound expression inside {$cond:[]} - something like:

    collection.aggregate(
        { 
            $match : { 
                '_id' : {$in:ids}
            } 
        },
        {
            $group: {
                _id: '$someField',
                ...
                count: {$sum: { $cond: [ {$and : [ { $eq: [ "$otherField", false] },
                                                   { $eq: [ "$anotherField","value"] }
                                         ] },
                                         1,
                                         0 ] }}
            }
        },
        function(err, result){
            ...
        }
    );
    

    The $and operator is documented here: http://docs.mongodb.org/manual/reference/operator/aggregation/#boolean-operators

    0 讨论(0)
提交回复
热议问题