Database Manual / Reference / Query Language / Expressions

$map (expression operator)(表达式运算符)

Definition定义

$map
Applies an expression to each item in an array and returns an array with the applied results.表达式应用于数组中的每个项,并返回一个包含应用结果的数组。

Compatibility兼容性

You can use $map for deployments hosted in the following environments:您可以将$map用于在以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The $map expression has the following syntax:$map表达式具有以下语法:

{ $map: { input: <expression>, as: <string>, in: <expression> } }
Field字段Specification规格
input

An expression that resolves to an array.解析为数组的表达式

If input resolves to null or refers to a missing field, $map returns null.如果input解析为null或引用缺少的字段,$map将返回null

If input resolves to a non-array, non-null value, the pipeline errors.如果input解析为非数组、非null值,则管道错误。

asOptional. 可选。A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this.表示input数组中每个单独元素的变量的名称。如果未指定名称,则变量名默认为this
inAn expression that is applied to each element of the input array. The expression references each element individually with the variable name specified in as.应用于input数组的每个元素的表达式。表达式使用中指定的变量名单独引用每个元素。

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Examples示例

Add to Each Element of an Array添加到数组的每个元素

In mongosh, create a sample collection named grades with the following documents:mongosh中,使用以下文档创建一个名为grades的示例集合:

db.grades.insertMany( [
{ quizzes: [ 5, 6, 7 ] },
{ quizzes: [ ] },
{ quizzes: [ 3, 8, 9 ] }
] )

The following aggregation operation uses $map with the $add expression to increment each element in the quizzes array by 2.以下聚合操作使用$map$add表达式将quizzes数组中的每个元素递增2

db.grades.aggregate( [
{
$project: {
adjustedGrades: {
$map: {
input: "$quizzes",
as: "grade",
in: { $add: [ "$$grade", 2 ] }
}
}
}
}
] )

This operation returns the following results:此操作返回以下结果:

[
{
_id: ObjectId("6390b8f7237da390c6869a62"),
adjustedGrades: [ 7, 8, 9 ]
},
{
_id: ObjectId("6390b8f7237da390c6869a63"),
adjustedGrades: []
},
{
_id: ObjectId("6390b8f7237da390c6869a64"),
adjustedGrades: [ 5, 10, 11 ]
}
]

Truncate Each Array Element截断每个数组元素

In mongosh, create a sample collection named deliveries with the following documents:mongosh中,使用以下文档创建一个名为deliveries的示例集合:

db.deliveries.insertMany( [
{
"city" : "Bakersfield",
"distances" : [ 34.57, 81.96, 44.24 ]
},
{
"city" : "Barstow",
"distances" : [ 73.28, 9.67, 124.36 ]
},
{
"city" : "San Bernadino",
"distances" : [ 16.04, 3.25, 6.82 ]
}
] )

The following aggregation operation uses $map to truncate each element in the distances array to its integer.以下聚合操作使用$map将距离数组中的每个元素截断为整数。

db.deliveries.aggregate( [
{
$project: {
city: "$city",
integerValues: {
$map: {
input: "$distances",
as: "decimalValue",
in: { $trunc: "$$decimalValue" }
}
}
}
}
] )

This operation returns the following results:此操作返回以下结果:

[
{
_id: ObjectId("6390b9b1237da390c6869a65"),
city: 'Bakersfield',
integerValues: [ 34, 81, 44 ]
},
{
_id: ObjectId("6390b9b1237da390c6869a66"),
city: 'Barstow',
integerValues: [ 73, 9, 124 ]
},
{
_id: ObjectId("6390b9b1237da390c6869a67"),
city: 'San Bernadino',
integerValues: [ 16, 3, 6 ]
}
]

Convert Celsius Temperatures to Fahrenheit将摄氏温度转换为华氏温度

In mongosh, create a sample collection named temperatures with the following documents:mongosh中,使用以下文档创建一个名为temperatures的样本集合:

db.temperatures.insertMany( [
{
"date" : ISODate("2019-06-23"),
"tempsC" : [ 4, 12, 17 ]
},
{
"date" : ISODate("2019-07-07"),
"tempsC" : [ 14, 24, 11 ]
},
{
"date" : ISODate("2019-10-30"),
"tempsC" : [ 18, 6, 8 ]
}
] )

The following aggregation operation uses the $addFields stage to add a new field to the documents called tempsF which contains Fahrenheit equivalents of the elements in the tempsC array. 以下聚合操作使用$addFields阶段向名为tempsF的文档中添加一个新字段,该字段包含tempsC数组中元素的华氏等效值。To convert from Celsius to Fahrenheit, the operation uses $map to $multiply the Celsius values by 9/5 and then $add 32.要从摄氏度转换为华氏度,该操作使用$map将摄氏度值乘以9/5,然后加32

 db.temperatures.aggregate( [
{
$addFields: {
"tempsF": {
$map: {
input: "$tempsC",
as: "tempInCelsius",
in: {
$add: [ { $multiply: [ "$$tempInCelsius", 9/5 ] }, 32 ]
}
}
}
}
}
] )

This operation returns the following results:此操作返回以下结果:

[
{
_id: ObjectId("6390ba11237da390c6869a68"),
date: ISODate("2019-06-23T00:00:00.000Z"),
tempsC: [ 4, 12, 17 ],
tempsF: [ 39.2, 53.6, 62.6 ]
},
{
_id: ObjectId("6390ba11237da390c6869a69"),
date: ISODate("2019-07-07T00:00:00.000Z"),
tempsC: [ 14, 24, 11 ],
tempsF: [ 57.2, 75.2, 51.8 ]
},
{
_id: ObjectId("6390ba11237da390c6869a6a"),
date: ISODate("2019-10-30T00:00:00.000Z"),
tempsC: [ 18, 6, 8 ],
tempsF: [ 64.4, 42.8, 46.4 ]
}
]

Learn More了解更多

To learn more about expressions used in the previous examples, see:要了解有关前面示例中使用的表达式的更多信息,请参阅: