简体   繁体   English

MongoDB C# 驱动程序多字段查询

[英]MongoDB C# Driver multiple field query

Using the MongoDB C# driver How can I include more than one field in the query (Im using vb.net)使用 MongoDB C# 驱动程序如何在查询中包含多个字段(我使用 vb.net)

I know how to do (for name1=value1 )我知道怎么做(对于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 ?如何修改此查询,以便我可以找到name1=value1name2=value2所有文档?

( Similar to ) ( 相似 )

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

Use And method - C# example from tutorial : 使用And方法 - 教程中的 C#示例:

MongoCollection<BsonDocument> books;
var query = Query.And(
    Query.EQ("author", "Kurt Vonnegut"),
    Query.EQ("title", "Cats Craddle")
);

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

And doesn't always do what you want (as I found was the case when doing a not operation on top of an and).并且并不总是做你想做的(正如我发现在 and 之上执行 not 操作时的情况)。 You can also create a new QueryDocument, as shown below.您还可以创建一个新的 QueryDocument,如下所示。 This is exactly the equivalent of what you were looking for.这与您要查找的内容完全相同。

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

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参考: https : //www.mongodb.com/blog/post/quick-start-csharp-and-mongodb--update-operation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM