Docs Home / Node.js Driver

Specify a Query指定查询

Overview概述

Most CRUD operations allow you to narrow the set of matched documents by specifying matching criteria in a query document. Query documents contain one or more query operators that apply to specific fields which determine which documents to include in the result set.大多数CRUD操作允许您通过在查询文档中指定匹配条件来缩小匹配文档的范围。查询文档包含一个或多个查询运算符,这些运算符应用于特定字段,这些字段决定了要在结果集中包含哪些文档。

In a query document, you can match fields against literal values, such as { title: 'The Room' }, or you can compose query operators to express more complex matching criteria. In this guide, we cover the following categories of query operators in MongoDB and show examples on how to use them:在查询文档中,您可以将字段与文字值进行匹配,例如{ title: 'The Room' },或者您可以组合查询运算符来表达更复杂的匹配条件。在本指南中,我们介绍了MongoDB中的以下几类查询运算符,并展示了如何使用它们的示例:

To follow the examples in this guide, use the following code snippet to insert documents that describe fruits into the myDB.fruits collection:要遵循本指南中的示例,请使用以下代码片段将描述水果的文档插入到myDB.fruits集合中:

const myDB = client.db("myDB");
const myColl = myDB.collection("fruits");

await myColl.insertMany([
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 },
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "color": "yellow" },
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
]);

Note

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 Access Data From a Cursor 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 = myColl.find(query);
for await (const doc of cursor) {
console.dir(doc);
}

This code snippet returns the following results:此代码片段返回以下结果:

{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }

Note

Literal value queries are equivalent to the $eq comparison operator. As a result, the following two queries are equivalent:文字值查询等效于$eq比较运算符。因此,以下两个查询是等效的:

myColl.find({
rating: { $eq: 5 }
});
myColl.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 in which the qty field value is greater than 5 and prints them out:以下操作使用比较运算符$gt搜索数量字段值大于5的文档并将其打印出来:

// $gt means "greater than"意思是“大于”
const query = { qty: { $gt : 5 } };
const cursor = myColl.find(query);
for await (const doc of cursor) {
console.dir(doc);
}

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 = myColl.find(query);
for await (const doc of cursor) {
console.dir(doc);
}

This code snippet returns the following results:此代码片段返回以下结果:

{ "_id": 4, "name": "avocados", "qty": 3, "rating": 5 }
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3 }

Note

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. As a result, the following two queries are equivalent:每当查询文档包含多个元素时,这些元素都会通过隐式$and逻辑运算符组合在一起,以找出哪些文档与查询匹配。因此,以下两个查询是等效的:

myColl.find({
rating: { $eq: 5 },
qty: { $gt: 4 }
});
myColl.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 color field:元素运算符允许您根据字段的存在、不存在或类型进行查询。以下操作使用元素运算符$exists来搜索包含color字段的文档:

const query = { color: { $exists: true } };
const cursor = myColl.find(query);
for await (const doc of cursor) {
console.dir(doc);
}

This code snippet returns the following results:此代码片段返回以下结果:

{ "_id": 2, "name": "bananas", "qty": 7, "rating": 1, "color": "yellow" }

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 queries, when querying for documents in a collection. Common evaluation operators include $regex and $text. 估值运算符允许您在查询集合中的文档时执行更高级的逻辑,如正则表达式和文本查询。常见的求值运算符包括$regex$textThe following operation uses the evaluation operator $mod to search for documents in which the qty field value is divisible by 3 with a remainder of 0:以下操作使用求值运算符$mod搜索qty字段值可被3整除且余数为0的文档:

$mod means "modulo" and returns the remainder after division表示“模”,并在除法后返回余数
const query = { qty: { $mod: [ 3, 0 ] } };
const cursor = myColl.find(query);
for await (const doc of cursor) {
console.dir(doc);
}

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运算符的参考手册条目。