1

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?

Assassinbeast
  • 1,207
  • 1
  • 17
  • 33
  • 1
    Possibly because you don't need to. MongoDB does not care if the property is singular or an array of values and would simply do the equality match anyway. In fact [`$elemMatch`](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/) is only required with an "object" or any array when **"two or more conditions"** need to apply. Otherwise the underlying query path to a single property and value is simply a "dot notation" variant. – Neil Lunn Oct 24 '17 at 01:20
  • 1
    In summary. It's true that `$elemMatch` is often misunderstood. But unless you need `{ "FavoriteNumbers": { "$elemMatch": { "$gte": 3, "$lte": 7 } }` which "is" **two** conditions, then all the query issued need be is `{ "FavoriteNumbers": 7 }`, regardless of being stored in an array. Same for the "object": `{ "Pets.Name": "Husky" }`. That's how MongoDB actually works. – Neil Lunn Oct 24 '17 at 01:22
  • 1
    Hi @NeilLunn . You have marked my question as a duplicate, but that is not a duplicate, because my example is using C# and that other example is using Mongoose with NodeJS – Assassinbeast Oct 24 '17 at 12:49
  • Does not matter which language it is. The solution is the same for any implemenation. No `$elemMatch` required here. Simply supply the equality match and let MongoDB do it's thing. Question closed. – Neil Lunn Oct 24 '17 at 12:51
  • @NeilLunn Okay, but im not sure if you have seen it, but i have made an update at the bottom of my question. And that is how to make the equality strongly typed. – Assassinbeast Oct 24 '17 at 12:56
  • 1
    Saw it and as I said is does not apply. It's still a duplicate because the same answer does actually apply. You need to "apply yourself" and learn how things actually want. This has nothing to do with "strongly typed". Learn when you have been shown a correct answer, fix your code and more along please. – Neil Lunn Oct 24 '17 at 22:18
  • it does matter what language it is if you're filtering on tags – Chazt3n May 08 '19 at 19:25
  • 1
    @Assassinbeast I know its been 3 years but I found the answer. With the newest driver you have to use Filter.AnyEq(). Thats specifically for array fields. – Donny V. Mar 04 '21 at 15:37

0 Answers0