With aggregation
it's easy. You need to use $size
with $setIntersection
and then match all the documents with element at least 2 numbers in the intersected array:
db.C.aggregate([
{
$project: {
numbers: 1,
intersectedNumbers: {
$size: {
$setIntersection: ['$numbers', [5,15,25,35]]
}
}
}
},
{
$match: {
'intersectedNumbers': {
$gte: 2
}
}
}
])
With find
it's more problematic since you need two stages as in the aggregation
above, and it's not possible with find
.
But what you can do, if the A
array is dynamic, is to create a function that will return all possibiltes of "at least 2" elements, and then use $or
and $all
to use find
:
db.C.find({
$or: [
{numbers: {$all: [5,15]}},
{numbers: {$all: [5,25]}},
{numbers: {$all: [5,35]}},
{numbers: {$all: [15,25]}},
{numbers: {$all: [15,35]}},
{numbers: {$all: [25,35]}}
]
})