how do i receive an array comparing two other arrays using the aggregation framework?

You can simply use $setIntersection operator to keep only elements present in your array parameter.

db.collection.aggregate([
  {
    "$addFields": {
      "newArray": {
        $setIntersection: [
          [
            "abc",
            "456"
          ],
          "$array"
        ]
      }
    }
  }
])

You can test it here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top