Multiple values in array using Builders<BsonDocument>.Filter - C#

This is the statememnt i need to call from my C# code, i have the result for the below in my MongoDB compass. I may need to pass 10 to 15 ID based on the user selection
db.TestLogs.find( { ID_ST: { $in: [ 2043,2040 ]} } )

Can someone help me to understand how can i call this in my C# code using

var commonFilter = Builders.Filter.(“ID_ST”, "2043,2040 ");

Hello @Shagil_Gopi ,

Welcome to The MongoDB Community Forums! :wave:

To us $In, you can update your code according to below example, this will take 2 inputs where first is document field and second is the iEnumerable values to match with corresponding field.

var commonFilter = Builders<BsonDocument>.Filter.In("ID_ST", new[] { 2043,2040 });

To make it more readable you can try

IEnumerable<int> myEnumberable = new List<int>() { 2043, 2040 };
var commonFilter = Builders<BsonDocument>.Filter.In("ID_ST", myEnumberable);

Please let us know if this doesn’t suit your purpose

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.