Not sure how to use ElemMatch in C # for MongoDb (latest driver version)

I have a MongoDB collection in the following format:

{ "_id" : ObjectId("5692a3397d7518330416f8e5"), "supertagname" : "xxx", "inclusions" : [ "test", "blabla" ], "exclusions" : [ ] } 

and I'm trying to query for all documents where the "inclusion" array contains the value I'm looking for. Here is the code

 string t = "blabla"; // the string value I am looking for filter = Builders<BsonDocument>.Filter.ElemMatch( "inclusions", Builders<BsonDocument>.Filter.Eq("inclusions", t)); var matches = dictCollection.Find(filter).ToList(); foreach (BsonDocument doc in matches) {} 

matches.count always 0. What am I doing wrong?

thanks

+5
source share
1 answer

I think you can make it easier with a filter like this:

 var filter = Builders<BsonDocument>.Filter.AnyEq("inclusions", t); 

This will filter documents in which the inclusions array contains the value you are looking for.

http://mongodb.imtqy.com/mongo-csharp-driver/2.2/reference/driver/definitions/#array-operators

+5
source

Source: https://habr.com/ru/post/1240468/


All Articles