How to use $in or $nin in mongo aggregation $group $cond

后端 未结 3 1881
说谎
说谎 2021-02-14 04:19

I want to achieve $sum with $cond by having $or on property:

db.collectionName.aggregate(
{
   \"$group\": {
     \"_id\":\'$created_at\',
     \"count\": {\"$su         


        
3条回答
  •  情书的邮戳
    2021-02-14 04:34

    First you should create a list of $id using $map in project and then use setIsSubset in group as below :

    db.collectionName.aggregate([{
      "$project": {
        "idsArray": {
          "$map": {
            "input": ["A"],
            "as": "el",
            "in": {
              "$cond": [{
                  "$eq": ["$$el", "A"]
                }, "$id",
                null
              ]
            }
          }
        }
      }
    }, {
      "$group": {
        "_id": "$created_at",
        "count": {
          "$sum": 1
        },
        "count_failure": {
          "$sum": {
            "$cond": [{
                "$setIsSubset": ["$idsArray", [
                  0,
                  100,
                  101,
                  102,
                  103,
                  104,
                  105
                ]]
              },
              1,
              0
            ]
          }
        }
      }
    }])
    

提交回复
热议问题