Database Manual / Aggregation Operations / Aggregation Pipeline

Field Paths字段路径

You can use field path expressions to access fields in input documents. To specify a field path, prefix the field name or the dotted field path (if the field is in an embedded document) with a dollar sign $.您可以使用字段路径表达式访问输入文档中的字段。要指定字段路径,请在字段名或点化的字段路径(如果字段位于嵌入式文档中)前加上美元符号$

Use Cases用例

You can use field paths for the following use cases:您可以将字段路径用于以下用例:

Nested Fields嵌套字段

The following example uses the planets collection from the Atlas Sample Databases. Each document in this collection has the following structure:以下示例使用Atlas示例数据库中的planets(行星)集合。此集合中的每个文档都具有以下结构:

{
_id: new ObjectId("6220f6b78a733c51b416c80e"),
name: "Uranus",
orderFromSun: 7,
hasRings: true,
mainAtmosphere: [ "H2", "He", "CH4" ],
surfaceTemperatureC: { min: null, max: null, mean: -197.2 }
}

To specify the nested field mean within the surfaceTemperatureC field, use dot notation ("field.nestedField") with a dollar sign $. 要指定surfaceTemperatureC字段内的嵌套字段mean,请使用带美元符号$的点符号("field.nestedField")。The following aggregation pipeline projects only the mean nested field value for each document:以下聚合管道仅投影每个文档的mean嵌套字段值:

db.planets.aggregate( [
{
$project: {
nested_field: "$surfaceTemperatureC.mean"
}
}
] )

Below is an example returned document:以下是返回文档的示例:

{ _id: ObjectId('6220f6b78a733c51b416c80e'), nested_field: -197.2 }

Array of Nested Fields嵌套字段数组

You can use dot notation in a field path to access a field that is nested within an array.您可以在字段路径中使用点符号来访问嵌套在数组中的字段。

For example, consider a products collection that contains an instock field. The instock field contains an array of nested warehouse fields.例如,考虑一个包含instock字段的products集合。instock字段包含一组嵌套的warehouse字段。

db.products.insertMany( [
{ item: "journal", instock: [ { warehouse: "A"}, { warehouse: "C" } ] },
{ item: "notebook", instock: [ { warehouse: "C" } ] },
{ item: "paper", instock: [ { warehouse: "A" }, { warehouse: "B" } ] },
{ item: "planner", instock: [ { warehouse: "A" }, { warehouse: "B" } ] },
{ item: "postcard", instock: [ { warehouse: "B" }, { warehouse: "C" } ] }
] )

The following aggregation pipeline uses $instock.warehouse to access the nested warehouse fields.以下聚合管道使用$instock.warehouse访问嵌套的warehouse字段。

db.products.aggregate( [
{
$project: {
item: 1,
warehouses: "$instock.warehouse"
}
}
] )

In this example, $instock.warehouse outputs an array of values that are in the nested warehouse field for each document. 在此示例中,$instock.warehouse输出每个文档的嵌套warehouse字段中的值数组。The pipeline returns the following documents:管道返回以下文档:

[
{
_id: ObjectId('6740b55e33b29cf6b1d884f7'),
item: "journal",
warehouses: [ "A", "C" ]
},
{
_id: ObjectId('6740b55e33b29cf6b1d884f8'),
item: "notebook",
warehouses: [ "C" ]
},
{
_id: ObjectId('6740b55e33b29cf6b1d884f9'),
item: "paper",
warehouses: [ "A", "B" ]
},
{
_id: ObjectId('6740b55e33b29cf6b1d884fa'),
item: "planner",
warehouses: [ "A", "B" ]
},
{
_id: ObjectId('6740b55e33b29cf6b1d884fb'),
item: "postcard",
warehouses: [ "B", "C" ]
}
]

Array of Nested Arrays嵌套数组数组

You can also use dot notation with a dollar sign $ in a field path to access an array within a nested array.您还可以在字段路径中使用带美元符号$点表示法来访问嵌套数组中的数组。

This example uses a fruits collection that contains the following document:此示例使用包含以下文档的fruits集合:

db.fruits.insertOne(
{
_id: ObjectId("5ba53172ce6fa2fcfc58e0ac"),
inventory: [
{
apples: [
"macintosh",
"golden delicious",
]
},
{
oranges: [
"mandarin",
]
},
{
apples: [
"braeburn",
"honeycrisp",
]
}
]
}
)

The document in the collection contains an inventory array where each element in the array is an object that contains a nested array field.集合中的文档包含一个inventory数组,其中数组中的每个元素都是一个包含嵌套数组字段的对象。

Consider the following aggregation pipeline:考虑以下聚合管道:

db.fruits.aggregate( [
{ $project:
{ all_apples: "$inventory.apples" } }
] )

In this pipeline, $inventory.apples resolves to an array of nested arrays. The pipeline returns the following document:在这个管道中,$inventory.apples解析为一个嵌套数组数组。管道返回以下文档:

{
_id: ObjectId('5ba53172ce6fa2fcfc58e0ac'),
all_apples: [
[ "macintosh", "golden delicious" ],
[ "braeburn", "honeycrisp" ]
]
}

Learn More了解更多

For more information on accessing and interacting with nested elements, see Dot Notation and Query an Array of Embedded Documents.有关访问嵌套元素和与嵌套元素交互的更多信息,请参阅点表示法查询嵌入式文档数组