This page provides examples in:本页提供了以下示例:
This page provides examples of query operations on embedded/nested documents 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 on embedded/nested documents 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 on embedded/nested documents using the pymongo.collection.Collection.find()
method in the PyMongo Python driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents 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 on embedded/nested documents 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 on embedded/nested documents using the MongoDB\Collection::find()
method in the MongoDB PHP Library. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.此页面提供使用mongodb Java反应流驱动程序中的com.mongodb.reactivestreams.client.MongoCollection.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 on embedded/nested documents using the MongoCollection.Find() method in the MongoDB C# Driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the inventory
collection. To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of query operations on embedded/nested documents using the Collection.Find function in the MongoDB Go Driver. The examples on this page use the inventory
collection. 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 specify an equality condition on a field that is an embedded/nested document, use the query filter document 要在嵌入/嵌套文档的字段上指定相等条件,请使用查询筛选文档{ <field>: <value> }
where <value>
is the document to match.{ <field>: <value> }
,其中<value>
是要匹配的文档。
To specify an equality condition on a field that is an embedded/nested document, use the query filter document 要在嵌入/嵌套文档的字段上指定相等条件,请使用查询筛选文档{ <field>: <value> }
where <value>
is the document to match.{ <field>: <value> }
,其中<value>
是要匹配的文档。
To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field>: <value> }
where <value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the filter document 要在嵌入/嵌套文档的字段上指定相等条件,请使用筛选文档eq( <field1>, <value>)
where <value>
is the document to match.eq( <field1>, <value>)
,其中<value>
是要匹配的文档。
To specify an equality condition on a field that is an embedded/nested document, use the query filter document 要在嵌入/嵌套文档的字段上指定相等条件,请使用查询筛选文档{ <field>: <value> }
where <value>
is the document to match.{ <field>: <value> }
,其中<value>
是要匹配的文档。
To specify an equality condition on a field that is an embedded/nested document, use the query filter document [ <field> => <value> ]
where <value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field>: <value> }
where <value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the filter document 要在嵌入/嵌套文档的字段上指定相等条件,请使用筛选文档eq( <field1>, <value>)
where <value>
is the document to match.eq( <field1>, <value>)
,其中<value>
是要匹配的文档。
To specify an equality condition on a field that is an embedded/nested document, construct a filter using the Eq method:
<value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field> => <value> }
where <value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the query filter document { <field> => <value> }
where <value>
is the document to match.
To specify an equality condition on a field that is an embedded/nested document, use the filter document equal( <field1>, <value> )
where <value>
is the document to match.
For example, the following query selects all documents where the field 例如,以下查询选择字段size
equals the document { h: 14, w: 21, uom: "cm" }
:size
等于文档{ h: 14, w: 21, uom: "cm" }
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
Equality matches on the whole embedded document require an exact
match of the specified 整个嵌入文档的相等匹配要求精确匹配指定的<value>
document, including the field order. <value>
文档,包括字段顺序。For example, the following query does not match any documents in the 例如,以下查询与inventory
collection:inventory
集合中的任何文档都不匹配:
To specify a query condition on fields in an embedded/nested document, use dot notation (要指定嵌入/嵌套文档中字段的查询条件,请使用点表示法("field.nestedField"
)."field.nestedField"
)。
Note
When querying using dot notation, the field and nested field must be inside quotation marks.使用点表示法查询时,字段和嵌套字段必须位于引号内。
The following example selects all documents where the field 以下示例选择uom
nested in the size
field equals "in"
:size
字段中嵌套的字段uom
等于"in"
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下筛选器复制到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. 除了相等条件外,MongoDB还提供了各种查询运算符来指定过滤条件。Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. 使用com.mongodb.client.model.Filters
助手方法来帮助创建筛选文档。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 query uses the less than operator (以下查询在大小字段中嵌入的字段$lt
) on the field h
embedded in the size
field:h
上使用小于运算符($lt
):
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
AND
ConditionAND
条件¶The following query selects all documents where the nested field 以下查询选择嵌套字段h
is less than 15
, the nested field uom
equals "in"
, and the status
field equals "D"
:h
小于15
、嵌套字段uom
等于"in"
、status
字段等于"D"
的所有文档:
Copy the following filter into the Compass query bar and click Find:将以下过滤器复制到Compass查询栏中,然后单击“查找”:
For additional query examples, see:有关其他查询示例,请参阅: