I keep banging my head against the wall trying to solve the following problem (I'm using the new c# 2.0 driver):
The idea is to return all docs where the nested array is equal or a subset of a fixed array. Example:
Fixed array: [ "A", "B", "C" ]
container docs:
{
container1 { Name: "name1", Tags: [ "A", "B" ] },
container2 { Name: "name4", Tags: [ "A", "B", "C", "D" ] },
container3 { Name: "name2", Tags: [ "A" ] },
container4 { Name: "name3", Tags: [ "A", "B", "C" ] }
}
Based on the above data, the result should be:
{
container1 { Name: "name1", Tags: [ "A", "B" ] },
container3 { Name: "name2", Tags: [ "A" ] },
container4 { Name: "name3", Tags: [ "A", "B", "C" ] }
}
Notice how container2 was not part of the result set since [ "A", "B", "C", "D" ] is not a subset nor equal to [ "A", "B", "C" ]
Please if you have a non-2.0-C#-driver solution post it here anyways. It will help.
Much appreciated!!!
Use mongo Set Operator using $setIsSubset in aggregation you will get your result, check following query :
db.collectionName.aggregate({
"$project": {
"Name": 1,
"Tags": 1,
"match": {
"$setIsSubset": ["$Tags", ["A", "B", "C"]] //check Tags is subset of given array in your case array is ["A","B","C"]
}
}
}, {
"$match": {
"match": true // return only those matched true values
}
}, {
"$project": {
"Name": 1,
"Tags": 1
}
}).pretty()