19

Using the MongoDB C# driver How can I include more than one field in the query (Im using vb.net)

I know how to do (for name1=value1)

 Dim qry = Query.EQ("name1","value1")

How can I modify this query so I can make it find all documents where name1=value1 and name2=value2?

( Similar to )

db.collection.find({"name1":"value1","name2":"value2"})

3 Answers 3

17

I wanted to search a text in different fields and Full Text Search doesn't work for me even after wasting so much time. so I tried this.

var filter = Builders<Book>.Filter.Or(
    Builders<Book>.Filter.Where(p=>p.Title.ToLower().Contains(queryText.ToLower())),
    Builders<Book>.Filter.Where(p => p.Publisher.ToLower().Contains(queryText.ToLower())),
    Builders<Book>.Filter.Where(p => p.Description.ToLower().Contains(queryText.ToLower()))
            );
List<Book> books = Collection.Find(filter).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

This should be marked as the correct answer. Thank you for this, helped me out a great deal.
4

You can use:

var arrayFilter = Builders<BsonDocument>.Filter.Eq("student_id", 10000) 
 & Builders<BsonDocument>.Filter.Eq("scores.type", "quiz");

Reference: https://www.mongodb.com/blog/post/quick-start-csharp-and-mongodb--update-operation

Comments

2

And doesn't always do what you want (as I found was the case when doing a not operation on top of an and). You can also create a new QueryDocument, as shown below. This is exactly the equivalent of what you were looking for.

 Query.Not(new QueryDocument { 
    { "Results.Instance", instance }, 
    { "Results.User", user.Email } }))

Comments

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.