Run the following pipeline which uses the $filter operator to return a subset of the history array that passes a given specified condition. This condition makes use of the $setIntersection set operator to check the embedded plugin array if it intersects with the given input comparison array.
So for instance, the expression
{ $setIntersection: [ [ 1, 4 ], [ 2, 7 ] ] }
will return an empty set (array []) since the two arrays do not intersect. Use this as basis to compare the result with the $ne comparison operator. This will return true when the compared two values are not equivalent and false when they are equivalent. Use this result to feed the "cond" expression so that it will filter the appropriate elements.
The following pipeline demonstrates this:
var arr = [2,7];
db.collection.aggregate([
{ "$match": { "_id": ObjectId("57ffe28591f567293497d924") } }, // <-- filter here
{
"$project": {
"history": {
"$filter": {
"input": "$history",
"as": "item",
"cond": {
"$ne": [
{ "$setIntersection": ["$$item.plugin", arr] },
[]
]
}
}
}
}
}
])
Sample Output
{
"_id" : ObjectId("57ffe28591f567293497d924"),
"history" : [
{
"plugin" : [
1,
2,
3,
4
]
},
{
"plugin" : [
2,
3,
4,
6
]
}
]
}
As an alternative solution (if your MongoDB version does not support the $filter operator), consider using a combination of the $setDifference and $map operators to return the filtered array:
var arr = [2,7];
db.collection.aggregate([
{ "$match": { "_id": ObjectId("57ffe28591f567293497d924") } }, // <-- filter here
{
"$project": {
"history": {
"$setDifference": [
{
"$map": {
"input": "$history",
"as": "item",
"in": {
"$cond": [
{
"$ne": [
{ "$setIntersection": ["$$item.plugin", arr] },
[]
]
},
"$$item",
false
]
}
}
},
[false]
]
}
}
}
])