Query Documents查询文档
On this page本页内容
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.
This page provides examples of query operations using the Collection.find() method in the MongoDB Node.js Driver.本页提供了使用MongoDB Node.js驱动程序中的Collection.find()
方法进行查询操作的示例。
The examples on this page use the 此页面上的示例使用inventory
collection. Connect to a test database in your MongoDB instance then create the inventory
collection:inventory
集合。连接到MongoDB实例中的测试数据库,然后创建inventory
集合:
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);
Select All Documents in a Collection选择集合中的所有文档
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. 若要选择集合中的所有文档,请将一个空文档作为查询筛选器参数传递给查找方法。The query filter parameter determines the select criteria:查询筛选器参数确定选择条件:
const cursor = db.collection('inventory').find({});
This operation uses a filter predicate of 此操作使用{}
, which corresponds to the following SQL statement:{}
的筛选器谓词,该谓词对应于以下SQL语句:
SELECT * FROM inventory
Specify Equality Condition指定相等条件
To specify equality conditions, use 要指定相等条件,请在查询筛选器文档中使用<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
{ <field1>: <value1>, ... }
The following example selects from the 以下示例从inventory
collection all documents where the status
equals "D"
:inventory
集合中选择status
等于"D"
的所有文档:
const cursor = db.collection('inventory').find({ status: 'D' });
This operation uses a filter predicate of 此操作使用{ status: "D" }
, which corresponds to the following SQL statement:{ status: "D" }
的筛选器谓词,该谓词对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "D"
The MongoDB Compass query bar autocompletes the current query based on the keys in your collection's documents, including keys in embedded sub-documents.MongoDB Compass查询栏根据集合文档中的键(包括嵌入子文档中的键)自动完成当前查询。
Specify Conditions Using Query Operators使用查询运算符指定条件
A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:
{ <field1>: { <operator1>: <value1> }, ... }
The following example retrieves all documents from the 以下示例从inventory
collection where status
equals either "A"
or "D"
:inventory
集合中检索status
等于"A"
或"D"
的所有文档:
const cursor = db.collection('inventory').find({
status: { $in: ['A', 'D'] }
});
The operation uses a filter predicate of 该操作使用{ status: { $in: [ "A", "D" ] } }
, which corresponds to the following SQL statement:{ status: { $in: [ "A", "D" ] } }
的筛选器谓词,该谓词对应于以下SQL语句:
SELECT * FROM inventory WHERE status in ("A", "D")
Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.有关MongoDB查询运算符的完整列表,请参阅查询和投影运算符文档。
Specify AND
Conditions指定AND
条件
AND
ConditionsA compound query can specify conditions for more than one field in the collection's documents. 复合查询可以为集合文档中的多个字段指定条件。Implicitly, a logical 隐含地,逻辑AND
conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.AND
连接符连接复合查询的子句,以便查询选择集合中符合所有条件的文档。
The following example retrieves all documents in the 以下示例检索inventory
collection where the status
equals "A"
and qty
is less than ($lt
) 30
:inventory
集合中status
等于"A"
且qty
小于($lt
)30
的所有文档:
const cursor = db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 }
});
The operation uses a filter predicate of 该操作使用{ status: "A", qty: { $lt: 30 } }
, which corresponds to the following SQL statement:{ status: "A", qty: { $lt: 30 } }
的筛选器谓词,该谓词对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" AND qty < 30
See comparison operators for other MongoDB comparison operators.请参阅其他MongoDB比较运算符的比较运算符。
Specify OR
Conditions指定OR
条件
OR
ConditionsUsing the 使用$or
operator, you can specify a compound query that joins each clause with a logical OR
conjunction so that the query selects the documents in the collection that match at least one condition.$or
运算符,可以指定一个复合查询,该查询将每个子句与逻辑OR
连接起来,以便查询选择集合中至少匹配一个条件的文档。
The following example retrieves all documents in the collection where the 以下示例检索集合中status
equals "A"
or qty
is less than ($lt
) 30
:status
等于"A"
或qty
小于($lt
)30
的所有文档:
const cursor = db.collection('inventory').find({
$or: [{ status: 'A' }, { qty: { $lt: 30 } }]
});
The operation uses a filter predicate of 该操作使用{ $or: [ { status: 'A' }, { qty: { $lt: 30 } } ] }
, which corresponds to the following SQL statement:{ $or: [ { status: 'A' }, { qty: { $lt: 30 } } ] }
的筛选器谓词,该谓词对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" OR qty < 30
Queries that use comparison operators are subject to Type Bracketing.使用比较运算符的查询需要使用类型括号。
Specify AND
as well as OR
Conditions指定AND
和OR
条件
AND
as well as OR
ConditionsIn the following example, the compound query document selects all documents in the collection where the 在以下示例中,复合查询文档选择集合中status
equals "A"
and either qty
is less than ($lt
) 30
or item
starts with the character p
:status
等于"A"
且要么qty
小于($lt
)30
、要么item
以字符p
开头的所有文档:
const cursor = db.collection('inventory').find({
status: 'A',
$or: [{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }]
});
The operation uses a filter predicate of:该操作使用的筛选器谓词为:
{
status: 'A',
$or: [
{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }
]
}
which corresponds to the following SQL statement:其对应于以下SQL语句:
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
Additional Query Tutorials其他查询教程
For additional query examples, see:有关其他查询示例,请参阅:
Behavior行为
Cursor游标
The Collection.find() method returns a cursor.Collection.find()
方法返回一个游标。
Read Isolation读取隔离
For reads to Replica sets and replica set shards, read concern allows clients to choose a level of isolation for their reads. For more information, see Read Concern.对于对副本集和副本集分片的读取,读取关注允许客户端为其读取选择隔离级别。有关更多信息,请参阅读取关注。
Query Result Format查询结果格式
When you run a find operation with a MongoDB driver or 当您使用MongoDB驱动程序或mongosh
, the command returns a cursor that manages query results. The query results are not returned as an array of documents.mongosh
运行查找操作时,该命令将返回一个管理查询结果的游标。查询结果不会作为文档数组返回。
To learn how to iterate through documents in a cursor, refer to your driver's documentation. If you are using 要了解如何在游标中迭代文档,请参阅您的驱动程序文档。如果您正在使用mongosh
, see Iterate a Cursor in mongosh
.mongosh
,请参阅在mongosh
中迭代游标。