1

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);

1 Answer 1

1

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 });
Sign up to request clarification or add additional context in comments.

3 Comments

it seems you are right, it should be the way you have shown but it is not at the moment. Better fix that somehow and then try find().
thank you for pointing out my mistake on Tags. Inserting Tags property in the very first place to mongodb, sorted out all my find issues.
@Lapsens Glad to hear that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.