I have a MongoDB (running on Cosmos DB), and am trying to filter out sub-documents in an array, based on a list. I have had some success in the Mongo Shell but I did not achieve my goal, and no success in C# with the MongoDriver.
What I want to do is e.g. given these two documents in my database:
{
"person" : "John",
"pet" : [
{
"name" : "Spot",
"type" : "Dog"
},
{
"name" : "Ms. Whiskers",
"type" : "Cat"
},
{
"name" : "Jack",
"type" : "Hamster"
}
]
},
{
"person" : "Jane",
"pet" : [
{
"name" : "Max",
"type" : "Lizard"
}
]
}
I want to filter out the pet sub-documents where the type is neither a dog or a hamster.
{
"person" : "John",
"pet" : [
{
"name" : "Ms. Whiskers",
"type" : "Cat"
}
]
},
{
"person" : "Jane",
"pet" : [
{
"name" : "Max",
"type" : "Lizard"
}
]
}
The closest I've been was the following Shell command as $nin unknown operator error.
db.collection.aggregate([{
$project: {
pet: {
$filter: {
input: "$pet",
as: "p",
cond: {$ne: [ "$$p.type", "dog" ]}
}
}
}
}
]).pretty()
But I cannot figure out how to translate this to a C# Mongo Driver query, can anyone help?
The Cosmos emulates a version 3.6 MongoDB, which have just got a version bump to 4.0 if that does any difference for my scenario. I am using MongoDriver v2.12.2, and .NET Core 3.1