Query on Embedded/Nested 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 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
集合。Connect to a test database in your MongoDB instance then create the 连接到MongoDB实例中的测试数据库,然后创建inventory
collection: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'
}
]);
Match an Embedded/Nested Document匹配嵌入/嵌套文档
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>
是要匹配的文档。
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" }
的所有文档:
const cursor = db.collection('inventory').find({
size: { h: 14, w: 21, uom: 'cm' }
});
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
集合中的任何文档都不匹配:
const cursor = db.collection('inventory').find({
size: { w: 21, h: 14, uom: 'cm' }
});
Query on Nested Field查询嵌套字段
To specify a query condition on fields in an embedded/nested document, use dot notation (若要对嵌入/嵌套文档中的字段指定查询条件,请使用点表示法("field.nestedField"
)."field.nestedField"
)。
When querying using dot notation, the field and nested field must be inside quotation marks.使用点表示法进行查询时,字段和嵌套字段必须位于引号内。
Specify Equality Match on a Nested Field在嵌套字段上指定相等匹配
The following example selects all documents where the field 以下示例选择嵌套在uom
nested in the size
field equals "in"
:size
字段中的字段uom
等于"in"
的所有文档:
const cursor = db.collection('inventory').find({
'size.uom': 'in'
});
Specify Match using Query Operator使用查询运算符指定匹配
A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:
{ <field1>: { <operator1>: <value1> }, ... }
The following query uses the less than operator (以下查询在$lt
) on the field h
embedded in the size
field:size
字段中嵌入的字段h
上使用小于运算符($lt
):
const cursor = db.collection('inventory').find({
'size.h': { $lt: 15 }
});
Specify AND
Condition指定AND
条件
AND
ConditionThe 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"
的所有文档:
const cursor = db.collection('inventory').find({
'size.h': { $lt: 15 },
'size.uom': 'in',
status: 'D'
});
Additional Query Tutorials其他查询教程
For additional query examples, see:有关其他查询示例,请参阅: