Jullien
Jullien

Reputation: 11

Mongo db find string in a list with .net

I have several records in Posts collection which has Tags field as

"Tags" : [ "Xyr,zau,iRS" ]

and I want to find all posts containing tag I send to a function. What is the right way to do it?

Some of the things I have tried and coun't retrieve any data are,

tag as string parametre

var builder = Builders<Post>.Filter;
var filter = builder.Eq("Tags", tag);

var filter = new BsonDocument("Tags", new BsonDocument("$eq", tag));

var filter = new BsonDocument("Tags", new BsonDocument("$in", tag)); // That one somehow generated an error

var filter= new BsonDocument("Tags", tag);

Upvotes: 1

Views: 704

Answers (1)

jhmt
jhmt

Reputation: 1421

Assuming your Tags filed is like this:

"Tags" : [ "Xyr", "zau", "iRS" ]

Then you can use "$in" query in MongoDB driver 2.0 like this:
API Documentation

var filter = Builders<Post>.Filter.In("Tags", new string[] { tag });

Upvotes: 1

Related Questions