Specify a Query指定查询¶
On this page
Overview概述¶
Most CRUD operations allow you to narrow the set of matched documents by specifying matching criteria in a query document. 大多数CRUD操作允许您通过在查询文档中指定匹配条件来缩小匹配文档集。Query documents contain one or more query operators that apply to specific fields which determine which documents to include in the result set.查询文档包含一个或多个应用于特定字段的查询运算符,这些字段确定要在结果集中包含哪些文档。
In a query document, you can match fields against literal values (e.g. 在查询文档中,可以将字段与文字值(例如{ title: 'The Room' }
) or you can compose query operators to express more complex matching criteria. {title:'The Room'}
)匹配,也可以组合查询运算符来表示更复杂的匹配条件。In this guide, we cover the following categories of query operators in MongoDB and show examples on how to use them:在本指南中,我们将介绍MongoDB中的以下查询运算符类别,并举例说明如何使用它们:
Use the following code snippet to create a collection of documents that describe an inventory of fruit to follow along with our query operator examples:使用以下代码片段创建一组文档,这些文档描述了要遵循的水果库存以及我们的查询运算符示例:
await collection.insertMany([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "microsieverts": 0.1 },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]);
Your query operation may return a reference to a cursor that contains matching documents. 查询操作可能返回对包含匹配文档的游标的引用。To learn how to examine data stored in the cursor, see the Cursor Fundamentals page.要了解如何检查游标中存储的数据,请参阅游标基础知识页面。
Literal Value Queries文字值查询¶
Literal value queries allow you to query for data that exactly matches a value you provide in the query document. 文字值查询允许您查询与查询文档中提供的值完全匹配的数据。A literal value query has two parts: a field name and a value. 文字值查询有两部分:字段名和值。Documents returned from such a query must contain a field that has exactly the same name as the provided name and a value for that field that is exactly the same as the provided value. 从此类查询返回的文档必须包含与提供的名称完全相同的字段以及与提供的值完全相同的字段值。The following operation uses a literal query to search for documents containing a field called "name" that has a value of "apples":以下操作使用文字查询搜索包含名为“name”的字段(其值为“apples”)的文档:
const query = { "name": "apples" };
const cursor = collection.find(query);
await cursor.forEach(console.dir);
This code snippet returns the following results:此代码段返回以下结果:
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
Literal value queries are equivalent to the 文字值查询相当于$eq
comparison operator. $eq
比较运算符。As a result, the following two queries are equivalent:因此,以下两个查询是等效的:
collection.find({
rating: { $eq: 5 }
})
collection.find({
rating: 5
})
Comparison Operators比较运算符¶
Comparison operators allow you to query for data based on comparisons with values in a collection. 比较运算符允许您基于与集合中的值的比较来查询数据。Common comparison operators include 常见的比较运算符包括$gt
for "greater than" comparisons, $lt
for "less than" comparisons, and $ne
for "not equal to" comparisons. $gt
(大于)比较、$lt
(小于)比较和$ne
(不等于)比较。The following operation uses the comparison operator 以下操作使用比较运算符$gt
to search for documents with a quantity value greater than 5 and prints them out:$gt
搜索数量值大于5的文档并将其打印出来:
// $gt means "greater than"
const query = { qty: { $gt : 5 } };
const cursor = collection.find(query);
await cursor.forEach(console.dir);
This code snippet returns the following results:此代码段返回以下结果:
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1 }
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
Logical Operators逻辑运算符¶
Logical operators allow you to query for data using logic applied to the results of field-level operators. 逻辑运算符允许您使用应用于字段级运算符结果的逻辑查询数据。For instance, you can use the 例如,可以使用$or
method to query for documents that match either a $gt
comparison operator or a literal value query. $or
方法查询与$gt
比较运算符或文本值查询匹配的文档。The following operation uses the logical operator 以下操作使用逻辑运算符$not
to search for documents with a quantity value that is not greater than 5 and prints them out:$not
搜索数量值不大于5的文档并将其打印出来:
const query = { qty: { $not: { $gt: 5 }}};
const cursor = collection.find(query);
await cursor.forEach(console.dir);
This code snippet returns the following results:此代码段返回以下结果:
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }
Whenever a query document contains multiple elements, those elements are combined together with an implicit 每当查询文档包含多个元素时,这些元素都会与隐式$and
logical operator to figure out which documents match the query. $and
逻辑运算符组合在一起,以确定哪些文档与查询匹配。As a result, the following two queries are equivalent:因此,以下两个查询是等效的:
collection.find({
rating: { $eq: 5 },
qty: { $gt: 4 }
})
collection.find({
$and: [
{ rating: { $eq: 5 }},
{ qty: { $gt: 4 }}
]
})
For more information on comparison operators, see the reference manual entry for Comparison Query Operators.有关比较运算符的更多信息,请参阅比较查询运算符的参考手册条目。
Element Operators元素运算符¶
Element operators allow you to query based on the presence, absence, or type of a field. 元素运算符允许您根据字段的存在、不存在或类型进行查询。The following operation uses the element operator 以下操作使用元素运算符$exists
to search for documents containing the microsieverts
field:$exists
搜索包含microsieverts
字段的文档:
const query = { microsieverts: { $exists: true } };
const cursor = collection.find(query);
await cursor.forEach(console.dir);
This code snippet returns the following results:此代码段返回以下结果:
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "microsieverts": 0.1 }
For more information on this operator, see the reference manual entry for the $exists operator.有关此操作员的更多信息,请参阅$exists运算符的参考手册条目。
Evaluation Operators估值运算符¶
Evaluation operators allow you to execute higher level logic, like regex and text searches, when querying for documents in a collection. 当查询集合中的文档时,求值运算符允许您执行更高级别的逻辑,如正则表达式和文本搜索。Common evaluation operators include 常见的求值运算符包括$regex
and $text
. $regex
和$text
。The following operation uses the evaluation operator 以下操作使用求值运算符$mod
to search for documents with a quantity value that is divisible by 3 with a remainder of 0:$mod
搜索数量值可被3整除,余数为0的文档:
// $mod means "modulo" and returns the remainder after division
const query = { qty: { $mod: [ 3, 0 ] } };
const cursor = collection.find(query);
await cursor.forEach(console.dir);
This code snippet returns the following results:此代码段返回以下结果:
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 }
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
For more information on this operator, see the reference manual entry for the $mod operator.有关此操作员的更多信息,请参阅$mod运算符的参考手册条目。