$cond inside an array is not working

db.group.insertMany([
  {
    "name": "a",
    "users": [
      {
        "uname": "A"
      },
      {
        "uname": "B"
      },
      {
        "uname": "C"
      }
    ]
  },
  {
    "name": "b",
    "users": [
      {
        "uname": "A"
      },
      {
        "uname": "D"
      },
      {
        "uname": "E"
      }
    ]
  }
]);

Now I want to add a new Field ‘isAPresent’ which checks whether a user with username ‘A’ is present in each group.

db.group.aggregate([
    {  
        $addFields: {     
            isAPresent: {
                $cond:[ {
                    $eq:['uname','A']
                },true,false]
            }
        }
          
    }
]);

This always giving me false, even though “A” is present in the first and second group

Hi,

You can do it with $in operator, like this:

db.collection.aggregate([
  {
    "$addFields": {
      "isAPresent": {
        "$cond": [
          {
            "$in": [
              "A",
              "$users.uname"
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

Working example