Docs HomeNode.js

Specify a Query指定查询

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, such as { 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中查询运算符的以下类别,并展示了如何使用它们的示例:

To follow along with 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 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 = 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. 文字值查询相当于$eq比较运算符。As a result, the following two queries are equivalent:因此,以下两个查询是等效的:

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 with a quantity value 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:因此,以下两个查询是等效的:

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 searches, 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 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$mod表示“取模”,并返回除法后的余数
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运算符的参考手册条目。