This page provides examples in:本页提供了以下示例:
This page provides examples of query operations using the 此页面提供了使用db.collection.find()
method in the mongo
shell. mongo
shell中的db.collection.find()
方法进行查询操作的示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using MongoDB Compass. 本页提供使用MongoDB Compass的查询操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。Populate the 使用以下文档填充inventory
collection with the following documents:inventory
集合:
This page provides examples of query operations using the pymongo.collection.Collection.find()
method in the PyMongo Python driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.本页提供了使用mongodb Java同步驱动程序中的com.mongodb.client.MongoCollection.find方法进行查询操作的示例。
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. 驱动程序提供com.mongodb.client.model.Filters
帮助程序方法,以方便创建筛选文档。The examples on this page use these methods to create the filter documents.本页上的示例使用这些方法创建筛选文档。
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
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. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the MongoDB\Collection::find()
method in the MongoDB PHP Library. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the MongoCollection.Find() method in the MongoDB C# Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations using the Collection.Find function in the MongoDB Go Driver. The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. 要选择集合中的所有文档,请将空文档作为查询筛选器参数传递给find方法。The query filter parameter determines the select criteria:查询过滤器参数确定选择条件:
To select all documents in the collection, pass an empty document as the query filter parameter to the query bar. 要选择集合中的所有文档,请将空文档作为查询筛选参数传递给查询栏。The query filter parameter determines the select criteria:查询过滤器参数确定选择条件:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. 要选择集合中的所有文档,请将空文档作为查询筛选器参数传递给find方法。The query filter parameter determines the select criteria:查询过滤器参数确定选择条件:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. 要选择集合中的所有文档,请将空文档作为查询筛选器参数传递给find方法。The query filter parameter determines the select criteria:查询过滤器参数确定选择条件:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. 要选择集合中的所有文档,请将空文档作为查询筛选器参数传递给find方法。The query filter parameter determines the select criteria:查询过滤器参数确定选择条件:
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:
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:
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:
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:
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:
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:
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:
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:
This operation corresponds to the following SQL statement:此操作对应于以下SQL语句:
For more information on the MongoDB Compass query bar, see Query Bar.有关MongoDB Compass查询栏的更多信息,请参阅查询栏。
For more information on the syntax of the method, see find()
.
For more information on the syntax of the method, see com.mongodb.client.MongoCollection.find.有关该方法语法的更多信息,请参阅com.mongodb.client.MongoCollection.find
。
For more information on the syntax of the method, see com.mongodb.reactivestreams.client.MongoCollection.find.有关该方法语法的更多信息,请参阅com.mongodb.reactivestreams.client.MongoCollection.find
。
For more information on the syntax of the method, see Find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see collection.find().
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
To specify equality conditions, use the 要指定相等条件,请使用com.mongodb.client.model.Filters.eq_
method to create the query filter document:com.mongodb.client.model.Filters.eq_
方法创建查询筛选器文档:
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field> => <value>
expressions in the query filter document:<field> => <value>
表达式:
To specify equality conditions, use 要指定相等条件,请使用查询筛选文档中的<field>:<value>
expressions in the query filter document:<field>:<value>
表达式:
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:要指定相等条件,请使用com.mongodb.client.model.Filters.eq
方法创建查询筛选文档:
To specify equality conditions, construct a filter using the Eq method:
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
The following example selects from the 以下示例从inventory
collection all documents where the status
equals "D"
:inventory
集合中选择status
为"D"
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
This operation corresponds to the following SQL statement:此操作对应于以下SQL语句:
Note
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查询栏根据集合文档中的键(包括嵌入子文档中的键)自动完成当前查询。
A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:
A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. 除了相等条件外,MongoDB还提供了各种查询运算符来指定过滤条件。Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. 使用com.mongodb.client.model.Filters
助手方法来帮助创建筛选文档。For example:例如:
A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:例如:
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:例如:
A query filter document can use the query operators to specify conditions in the following form:
A query filter document can use the query operators to specify conditions in the following form:
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:例如:
The following example retrieves all documents from the 以下示例从inventory
collection where status
equals either "A"
or "D"
:inventory
集合中检索status
为"A"
或"D"
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
Note
Although you can express this query using the 尽管可以使用$or
operator, use the $in
operator rather than the $or
operator when performing equality checks on the same field.$or
运算符表示此查询,但在对同一字段执行相等性检查时,请使用$in
运算符而不是$or
运算符。
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.有关MongoDB查询运算符的完整列表,请参阅查询和投影运算符文档。
AND
ConditionsAND
条件¶A 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
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
See comparison operators for other MongoDB comparison operators.有关其他MongoDB比较运算符,请参阅比较运算符。
OR
ConditionsOR
条件¶Using 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
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
Note
Queries which use comparison operators are subject to Type Bracketing.使用比较运算符的查询受类型括号的约束。
AND
as well as OR
ConditionsAND
以及OR
条件¶In 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
开头的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
The operation corresponds to the following SQL statement:该操作对应于以下SQL语句:
For additional query examples, see:有关其他查询示例,请参阅:
The db.collection.find()
method returns a cursor to the matching documents.db.collection.find()
方法将游标返回到匹配的文档。
The MongoDB Compass Find operation opens a cursor to the matching documents of the collection based on the find query.MongoDB Compass Find操作根据Find查询打开指向集合的匹配文档的游标。
For more information on sampling in MongoDB Compass, see the Compass FAQ.有关MongoDB Compass中采样的更多信息,请参阅Compass常见问题解答。
The pymongo.collection.Collection.find()
method returns a cursor to the matching documents. See the PyMongo documentation for iterating over a cursor.
The com.mongodb.client.MongoCollection.find method returns an instance of the com.mongodb.client.FindIterable interface.
The Collection.find() method returns a cursor.
The MongoDB\Collection::find()
method returns a cursor to the matching documents. See the MongoDB PHP Library documentation for iterating over a cursor.
com.mongodb.reactivestreams.client.MongoCollection.find returns an instance of the com.mongodb.reactivestreams.client.FindPublisher interface.
The MongoCollection.Find() method returns a cursor to the matching documents. See the MongoDB C# driver documentation for iterating over a cursor.
The MongoDB::Collection::find() method returns a cursor to the matching documents. See the MongoDB Perl driver documentation for iterating over a cursor.
The Mongo::Collection#find() method returns a CollectionView, which is an Enumerable
. A Cursor is created when the View
is enumerated; for example, by calling #to_enum()
on the View
. See the Ruby driver API documentation for iterating over a cursor.
The collection.find() method returns the find Observable.
The Collection.Find function returns a Cursor to the matching documents. See the Cursor documentation for more information.
New in version 3.2.版本3.2中的新功能。
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.有关更多信息,请参阅读关注点。
The following methods can also read documents from a collection:以下方法也可以从集合中读取文档:
db.collection.findOne
$match
pipeline stage provides access to MongoDB queries.$match
管道阶段提供对MongoDB查询的访问。Note
The db.collection.findOne()
method also performs a read operation to return a single document. db.collection.findOne()
方法还执行读取操作以返回单个文档。Internally, the 在内部,db.collection.findOne()
method is the db.collection.find()
method with a limit of 1.db.collection.findOne()
方法是限制为1的db.collection.find()
方法。
In addition to 除了filter
, MongoDB Compass also allows the following options to be passed to the query bar:filter
,MongoDB Compass还允许将以下选项传递到查询栏:
Project | |
Sort | |
Skip | |
Limit |
The following methods can also read documents from a collection:以下方法也可以从集合中读取文档:
pymongo.collection.Collection.find_one()
$match
pipeline stage provides access to MongoDB queries. See the PyMongo Aggregation Examples.Note
The pymongo.collection.Collection.find_one()
method also performs a read operation to return a single document. Internally, the pymongo.collection.Collection.find_one()
method is the pymongo.collection.Collection.find()
method with a limit of 1.
The following methods can also read documents from a collection:以下方法也可以从集合中读取文档:
$match
pipeline stage provides access to MongoDB queries. $match
管道阶段提供对MongoDB查询的访问。The following methods can also read documents from a collection:以下方法也可以从集合中读取文档:
$match
pipeline stage provides access to MongoDB queries. $match
管道阶段提供对MongoDB查询的访问。Note
The Collection.findOne() method also performs a read operation to return a single document. Collection.findOne()
方法还执行读取操作以返回单个文档。Internally, the Collection.findOne() method is the Collection.find() method with a limit of 1.在内部,Collection.findOne()
方法是限制为1的Collection.find()
方法。
The following methods can also read documents from a collection:
MongoDB\Collection::findOne()
$match
pipeline stage provides access to MongoDB queries. See the MongoDB PHP Library’s aggregation example.Note
The MongoDB\Collection::findOne()
method also performs a read operation to return a single document. Internally, the MongoDB\Collection::findOne()
method is the MongoDB\Collection::find()
method with a limit of 1.
The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See com.mongodb.reactivestreams.client.MongoCollection.aggregate for more information.The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See the MongoDB C# driver’s LINQ documentation.Note
The MongoCollection.FindOne() method also performs a read operation to return a single document. Internally, the MongoCollection.FindOne() method is the MongoCollection.Find() method with a limit of 1.
The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Perl driver’s aggregation examples.Note
The MongoDB::Collection::find_one() method also performs a read operation to return a single document. Internally, the MongoDB::Collection::find_one() method is the MongoDB::Collection::find() method with a limit of 1.
The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Ruby driver’s aggregation examples.The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See the MongoDB Scala driver’s aggregate method.The following methods can also read documents from a collection:
$match
pipeline stage provides access to MongoDB queries. See Collection.Aggregate.