1

I have document like this:

{
    "_id" : ObjectId("56ff6d54f07a36271465441c"),
    "Author" : "John",
    "Content" : "Thera are alot of databases",
    "CreatedAtUtc" : ISODate("2016-04-02T06:57:24.542Z"),
    "Tags" : [ 
        "elasticSearch", 
        "MongoDb", 
        "C#"
    ],
    "Title" : "Best DB"
}

I like to find all documents which have tag: "MongoDb"

I tried:

var filter = Builders<Post>.Filter.In("Tags", "MongoDb");
var posts = collection.Find(filter).ToList();

Where post is a C# class

But i get no documents. Any idea?

1 Answer 1

1

When querying for documents where there is an equality match of "MongoDB" on the Tags array, just use the query document { "Tags": "MongoDb" }:

Mongo Shell

db.collection.find({ "Tags": "MongoDb" })

C#

Use the Eq method to implement a filter document that specifies an equality condition:

var filter = Builders<Post>.Filter.Eq("Tags", "MongoDb");
var posts = collection.Find(filter).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

This actually resolves to { "Tags": ["MongoDB"] } which not only does not match to any element in the list, but also does not match any "list" that does not "soley" contain the "MongoDB" property. I don't think you really know the answer and just found a post you thought you would copy.
Thanks chridam, it's really too simple!.

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.