duplicates in array with condition of value

Solution 1:

You can try $expr expression operator with aggregation operators,

  • $filter to iterate loop of gists array and check for value "A"
  • $size to get total elements from above filtered result
  • $gt to check above size is greater than 1
  • $group by null and construct the array of car_id
db.collection.aggregate([
  {
    $match: {
      $expr: {
        $gt: [
          {
            $size: {
              $filter: {
                input: "$gists",
                cond: { $eq: ["$$this", "A"] }
              }
            }
          },
          1
        ]
      }
    }
  },
  {
    $group: {
      _id: null,
      car_id: { $push: "$car_id" }
    }
  }
])

Playground

Result:

[
  {
    "_id": null,
    "car_id": [79, 80]
  }
]