On this page本页内容
$map
Applies an expression to each item in an array and returns an array with the applied results.将表达式应用于数组中的每个项,并返回具有应用结果的数组。
The $map expression has the following syntax:$map表达式语法如下:
{ $map: { input: <expression>, as: <string>, in: <expression> } }
input | |
as | input array. this.this。
|
in | input array. as.as中指定的变量名分别引用每个元素。
|
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$map$map添加到数组的每个元素In 在mongosh, create a sample collection named grades with the following documents:mongosh中,使用以下文档创建名为grades的样本集合:
db.grades.insertMany([
{ _id: 1, quizzes: [ 5, 6, 7 ] },
{ _id: 2, quizzes: [ ] },
{ _id: 3, 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表达式将quezzes数组中的每个元素递增2。
db.grades.aggregate(
[
{ $project:
{ adjustedGrades:
{
$map:
{
input: "$quizzes",
as: "grade",
in: { $add: [ "$$grade", 2 ] }
}
}
}
}
]
)
This operation returns the following results:此操作返回以下结果:
{ "_id" : 1, "adjustedGrades" : [ 7, 8, 9 ] }
{ "_id" : 2, "adjustedGrades" : [ ] }
{ "_id" : 3, "adjustedGrades" : [ 5, 10, 11 ] }
$map$map截断每个数组元素In 在mongosh, create a sample collection named deliveries with the following documents:mongosh中,创建一个名为deliveries的示例集合,其中包含以下文档:
db.deliveries.insertMany([
{ "_id" : 1, "city" : "Bakersfield", "distances" : [ 34.57, 81.96, 44.24 ] },
{ "_id" : 2, "city" : "Barstow", "distances" : [ 73.28, 9.67, 124.36 ] },
{ "_id" : 3, "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将distances数组中的每个元素截断为其整数。
db.deliveries.aggregate(
[
{ $project:
{ city: "$city",
integerValues:
{ $map:
{
input: "$distances",
as: "decimalValue",
in: { $trunc: "$$decimalValue" }
}
}
}
}
]
)
This operation returns the following results:此操作返回以下结果:
{ "_id" : 1, "city" : "Bakersfield", "integerValues" : [ 34, 81, 44 ] }
{ "_id" : 2, "city" : "Barstow", "integerValues" : [ 73, 9, 124 ] }
{ "_id" : 3, "city" : "San Bernadino", "integerValues" : [ 16, 3, 6 ] }
$map$map将摄氏温度转换为华氏温度In 在mongosh, create a sample collection named temperatures with the following documents:mongosh中,使用以下文档创建名为temperatures的样本集合:
db.temperatures.insertMany([
{ "_id" : 1, "date" : ISODate("2019-06-23"), "tempsC" : [ 4, 12, 17 ] },
{ "_id" : 2, "date" : ISODate("2019-07-07"), "tempsC" : [ 14, 24, 11 ] },
{ "_id" : 3, "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将摄氏度值$multiply乘以9/5,然后$add加上 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" : 1, "date" : ISODate("2019-06-23T00:00:00Z"), "tempsC : [ 4, 12, 17 ], "tempsF" : [ 39.2, 53.6, 62.6 ] }
{ "_id" : 2, "date" : ISODate("2019-07-07T00:00:00Z"), "tempsC" : [ 14, 24, 11 ], "tempsF" : [ 57.2, 75.2, 51.8 ] }
{ "_id" : 3, "date" : ISODate("2019-10-30T00:00:00Z"), "tempsC" : [ 18, 6, 8 ], "tempsF" : [ 64.4, 42.8, 46.4 ] }