指定查询
概述
在本指南中,您可以了解如何使用 MongoDB .NET/C# 驱动程序指定查询。
您可以通过创建一个 查询过滤器 来缩小查询返回的匹配文档集。查询过滤器是一个表达式,用于指定在读取、更新或删除操作中要匹配的文档。
本指南中的示例使用名为guitars
:
{ "_id": 1, "make": "Fender", "models": ["Stratocaster", "Telecaster"], "establishedYear": 1946, "rating": 9 } { "_id": 2, "make": "Gibson", "models": ["Les Paul", "SG", "Explorer"], "establishedYear": 1902, "rating": 8 } { "_id": 3, "make": "PRS", "models": ["Silver Sky", "SE", "Custom"], "establishedYear": 1985, "rating": 9 } { "_id": 4, "make": "Kiesel", "models": ["Ares", "Vader", "Solo"], "establishedYear": 2015 } { "_id": 5, "make": "Ibanez", "models": ["RG", "AZ"], "establishedYear": 1957, "rating": 7 } { "_id": 6, "make": "Strandberg", "models": ["Boden", "Salen"], "establishedYear": 1982 }
以下 Guitar
类模型该集合中的文档。
public class Guitar { public int Id { get; set; } public string Make { get; set; } public List<string> Models { get; set; } public int EstablishedYear { get; set; } public int? Rating { get; set; } }
注意
在 guitars
集合中的文档使用驼峰式命名约定。本指南中的示例使用一个 ConventionPack
将集合中的字段反序列化为 PascalCase,并将它们映射到 Guitar
类的属性。
有关自定义序列化的更多信息,请参阅 自定义序列化。
有关类映射的更多信息,请参阅 类映射。
以下代码使用 Guitar
类作为类型参数实例化 _guitarsCollection
对象。此类型参数导致驱动程序自动将发送到 MongoDB 和从 MongoDB 接收的文档序列化和反序列化为 Guitar
类的实例
private static IMongoCollection<Guitar> _guitarsCollection;
文字值
文字值查询返回与您的查询过滤器完全匹配的文档。
以下示例将查询过滤器指定为 Find()
方法的参数。查询匹配所有 make
字段等于 "Fender" 的文档。
// Finds all documents with a "make" value of "Fender" var results = _guitarsCollection.Find(g => g.Make == "Fender").ToList(); foreach (var doc in results) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 1, "make" : "Fender", "models" : ["Stratocaster", "Telecaster"], "establishedYear" : 1946, "rating" : 9 }
以下示例使用构建器创建一个查询过滤器,该过滤器与前面示例匹配相同的文档
// Creates a filter for all documents with a "make" value of "Fender" var filter = Builders<Guitar>.Filter.Eq(g => g.Make, "Fender"); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 1, "make" : "Fender", "models" : ["Stratocaster", "Telecaster"], "establishedYear" : 1946, "rating" : 9 }
提示
查找所有文档
使用空查询过滤器来匹配集合中的所有文档。以下是如何使用构建器创建空查询过滤器的示例
var result = _guitarsCollection.Find(Builders<Guitar>.Filter.Empty).ToList();
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
比较运算符
比较运算符将文档中的值与查询过滤器中指定的值进行比较。常见的比较运算符包括
运算符 | 构建器 | 描述 |
---|---|---|
|
| 大于 |
|
| 小于或等于 |
|
| 不等于 |
有关比较运算符的完整列表,请参阅比较查询运算符 页面。
以下示例将查询过滤器指定为 Find()
方法的参数。查询匹配所有 establishedYear
字段大于 1985
的文档。
// Finds all documents with am "establishedYear" value greater than 1985 var results = _guitarsCollection.Find(g => g.EstablishedYear > 1985).ToList(); foreach (var doc in results) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 4, "make" : "Kiesel", "models" : ["Ares", "Vader", "Solo"], "establishedYear" : 2015, "rating" : null }
以下示例使用构建器创建一个查询过滤器,该过滤器与前面示例匹配相同的文档
// Creates a filter for all documents with an "establishedYear" value greater // than 1985 var filter = Builders<Guitar>.Filter.Gt(g => g.EstablishedYear, 1985); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 4, "make" : "Kiesel", "models" : ["Ares", "Vader", "Solo"], "establishedYear" : 2015, "rating" : null }
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
逻辑运算符
逻辑运算符通过逻辑运算对两个或多个表达式集的结果进行匹配。以下是一些逻辑运算符的列表
运算符 | 构建器 | 描述 |
---|---|---|
|
| 所有表达式都必须评估为 true。 |
|
| 至少有一个表达式必须评估为 true。 |
有关逻辑运算符的完整列表,请参阅逻辑查询运算符页面。
以下示例将查询过滤器指定为Find()
方法的参数。查询匹配所有establishedYear
字段大于或等于1985
,并且make
字段不等于 "Kiesel" 的文档。
// Finds all documents with an "establishedYear" value greater than 1985 // and a "make" value that is not equal to "Kiesel" var results = _guitarsCollection.Find(g => g.EstablishedYear >= 1985 && r.Make != "Kiesel").ToList(); foreach (var doc in results) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 3, "make" : "PRS", "models" : ["Silver Sky", "SE", "Custom"], "establishedYear" : 1985, "rating" : 9 }
以下示例使用构建器创建一个查询过滤器,该过滤器与前面示例匹配相同的文档
// Creates a filter for all documents with an "establishedYear" value greater // than 1985 and a "make" value that does not equal "Kiesel" var builder = Builders<Guitar>.Filter; var filter = builder.And(builder.Gte(g => g.EstablishedYear, 1985), builder.Ne(r => r.Make, "Kiesel")); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 3, "make" : "PRS", "models" : ["Silver Sky", "SE", "Custom"], "establishedYear" : 1985, "rating" : 9 }
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
数组运算符
数组运算符基于数组字段的值或元素数量匹配文档。以下是一些使用数组运算符的构建方法
运算符 | 描述 |
---|---|
| 如果数组字段包含查询中指定的所有元素,则匹配文档。 |
| 如果数组字段中的任何元素与指定的查询过滤器匹配,则匹配文档。 |
| 如果数组字段是指定的尺寸,则匹配文档。 |
注意
Any()
构建器使用$elemMatch
查询运算符。
有关$elemMatch
查询选择器的更多信息,请参阅$elemMatch.
有关数组运算符的更多信息,请参阅数组查询运算符页面。
以下示例使用构建器创建一个查询过滤器,匹配所有在models
字段中有3个元素的文档
// Creates a filter for all documents with 3 elements in the "models" field var filter = Builders<Guitar>.Filter.Size(g => g.Models, 3); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 2, "make" : "Gibson", "models" : ["Les Paul", "SG", "Explorer"], "establishedYear" : 1902, "rating" : 8 } { "_id" : 3, "make" : "PRS", "models" : ["Silver Sky", "SE", "Custom"], "establishedYear" : 1985, "rating" : 9 } { "_id" : 4, "make" : "Kiesel", "models" : ["Ares", "Vader", "Solo"], "establishedYear" : 2015, "rating" : null }
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
元素操作符
元素操作符根据字段的存在或类型查询数据。
有关元素操作符的完整列表,请参阅元素查询操作符页面。
以下示例使用构建器创建一个查询过滤器,匹配所有具有rating
字段的文档。
// Creates a filter for all documents with a populated "ratings" field var filter = Builders<Guitar>.Filter.Exists(g => g.Rating); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 1, "make" : "Fender", "models" : ["Stratocaster", "Telecaster"], "establishedYear" : 1946, "rating" : 9 } { "_id" : 2, "make" : "Gibson", "models" : ["Les Paul", "SG", "Explorer"], "establishedYear" : 1902, "rating" : 8 } { "_id" : 3, "make" : "PRS", "models" : ["Silver Sky", "SE", "Custom"], "establishedYear" : 1985, "rating" : 9 } { "_id" : 5, "make" : "Ibanez", "models" : ["RG", "AZ"], "establishedYear" : 1957, "rating" : 7 }
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
评估操作符
评估操作符分析单个字段或整个集合文档上的数据。使用评估操作符的一些构建器方法包括Regex()
和Text()
。
有关评估操作符的完整列表,请参阅评估查询操作符页面。
以下示例使用构建器创建一个查询过滤器,匹配所有在make
字段中具有以字母"G"开头的值的文档。
// Creates a filter for all documents with a populated "ratings" field var filter = Builders<Guitar>.Filter.Regex(g => g.Make, "^G"); // Finds all documents that match the filter var result = _guitarsCollection.Find(filter).ToList(); foreach (var doc in result) { // Prints the documents in bson (json) format Console.WriteLine(doc.ToBsonDocument()); }
{ "_id" : 2, "make" : "Gibson", "models" : ["Les Paul", "SG", "Explorer"], "establishedYear" : 1902, "rating" : 8 }
要了解更多关于使用构建器的信息,请参阅 使用构建器执行操作。
更多信息
有关本指南中提到的操作符的更多信息,请参阅以下服务器手册条目
要了解有关使用构建器的更多信息,请参阅使用构建器的操作。
要了解如何使用LINQ指定查询,请参阅LINQ。