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表达式语法如下所示:
input |
|
as |
input array. input数组中每个元素的变量的名称。this.this。 |
in |
input array. input数组的每个元素的表达式。as.as中指定的变量名分别引用每个元素。 |
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$map$map添加到数组的每个元素¶From the 在mongo shell, create a sample collection named grades with the following documents:mongo shell中,使用以下文档创建名为grades的样本集合:
The following aggregation operation uses 下面的聚合操作使用$map with the $add expression to increment each element in the quizzes array by 2.$map和$add表达式将quizzes数组中的每个元素增加2。
This operation returns the following results:此操作返回以下结果:
$map$map截断每个数组元素¶From the 从mongo shell, create a sample collection named deliveries with the following documents:mongo shell创建一个名为deliveries的样本集合,其中包含以下文档:
The following aggregation operation uses 下面的聚合操作使用$map to truncate each element in the distances array to its integer.$map将distance数组中的每个元素截断为其整数。
This operation returns the following results:此操作返回以下结果:
$map$map将摄氏温度转换为华氏温度¶From the 在mongo shell, create a sample collection named temperatures with the following documents:mongo shell中,创建一个名为temperatures的样本集合,其中包含以下文档:
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。
This operation returns the following results:此操作返回以下结果: