Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use in $in operator using the 2.0 version of the C# mongodb driver?

I found a lot of examples of how do use $in in the previous mongodb c# driver, but I can not find any examples on how to do it in the 2.0 version.

like image 403
BigJoe714 Avatar asked Dec 14 '22 11:12

BigJoe714


2 Answers

Use the AnyIn operator for the typed version:

Builders<TDocument>.Filter.AnyIn(x => x.Array,searchArray)

like image 82
Anwar Husain Avatar answered Jan 29 '23 21:01

Anwar Husain


There is $in operator in the driver:

var inValues = new List<string>() { "value1", "value2" };
var filter = Builders<BsonDocument>.Filter.In("Field Name", inValues);

// usage of the filter:
IMongoCollection<BsonDocument> collection = <get your collection here>
var query = collection.Find(filter)
...

where "Field Name" should be replaced with the actual field name used for filtering.

like image 33
Dima G Avatar answered Jan 29 '23 19:01

Dima G