Answers for "https://www.mongodb.com/community/forums/t/count-values-in-array-of-objects/13035/5"

0

https://www.mongodb.com/community/forums/t/count-values-in-array-of-objects/13035/5

db.collection.aggregate([
  {
    "$project": {
      "_id": 0,
      "up": {
        "$size": {
          "$filter": {
            "input": "$votes",
            "as": "vote",
            "cond": {
              "$eq": [
                "$$vote.action",
                "up"
              ]
            }
          }
        }
      },
      "down": {
        "$size": {
          "$filter": {
            "input": "$votes",
            "as": "vote",
            "cond": {
              "$eq": [
                "$$vote.action",
                "down"
              ]
            }
          }
        }
      }
    }
  }
])
Posted by: Guest on April-07-2022

Code answers related to "https://www.mongodb.com/community/forums/t/count-values-in-array-of-objects/13035/5"

Browse Popular Code Answers by Language