Lets say i have a document like this:
{ "Name" : "Danny", "FavoriteNumbers" : [ 3, 7, 8 ] }
And i want to get the document who has a name of Danny and a favorite number which is 8. Then i can do this:
var filter = Builders<Person>.Filter.Eq(x => x.Name, "Danny") &
Builders<Person>.Filter.ElemMatch(x => x.FavoriteNumbers, x => x == 10);
var people = collection.Find(filter).ToList();
But this will give me an exception which says: {document} is not supported
But it works if the array is an embedded document. For example, lets say my document now look like this:
{ "Name" : "Danny", "Pets" : [ { "Name" : "Husky" }, { "Name" : "Dino" } ] }
Now i want the Danny document, if Danny has a pet named Husky. So i do this:
var filter = Builders<Person>.Filter.Eq(x => x.Name, "Danny") &
Builders<Person>.Filter.ElemMatch(x => x.Pets, x => x.Name == "Husky");
var people = collection.Find(filter).ToList();
And this works because the array is an embedded document. No exception.
I have seen another thread here where, in the answer, the guy is using the same ElemMatch() method. But the values in the array are ints. So when i tested it, it didn't work for me either. So im not sure if the guy have even tested the code he posted himself.
Or am i doing something wrong? All help very appreciated :-)
Update:
The main reason why im using ElemMatch() and not Eq() is because i want to code with strongly typed text. And with Eq(), it seems i can only do it with strings.
So is there a solution so i can do it with strongly typed?