定义
$map对数组中的每个项目应用表达式,并返回包含已应用结果的数组。
兼容性
可以使用 $map 查找托管在以下环境中的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
The $map expression has the following syntax:
{ $map: { input: <expression>, as: <string>, arrayIndexAs: <string>, in: <expression> } }
字段 | 规范 |
|---|---|
| |
| 可选。 代表 |
| 可选.聚合变量的名称,表示当前元素在 您可以在表达式中使用变量名称。示例,如果指定 如果省略 For examples, see Access the Index of Each Item in an Array and Use 8.3版本新增。 |
| 应用于 |
有关表达式的更多信息,请参阅表达式。
示例
增加数组的每个元素
本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。
The following aggregation operation uses $map with the $add expression to add 10 to each element in the location.geo.coordinates array:
db.theaters.aggregate( [ { $match: { theaterId: { $in: [ 1000, 1003, 1008 ] } } }, { $project: { _id: 0, theaterId: 1, adjustedCoordinates: { $map: { input: "$location.geo.coordinates", as: "coord", in: { $add: [ "$$coord", 10 ] } } } } }, { $sort: { theaterId: 1 } } ] )
[ { theaterId: 1000, adjustedCoordinates: [ -83.24565, 54.85466 ] }, { theaterId: 1003, adjustedCoordinates: [ -66.512016, 48.29697 ] }, { theaterId: 1008, adjustedCoordinates: [ -111.96328, 48.367649 ] } ]
截断每个数组元素
The following aggregation operation uses $map to truncate each element in the location.geo.coordinates array to its integer:
db.theaters.aggregate( [ { $match: { theaterId: { $in: [ 1000, 1003, 1008 ] } } }, { $project: { _id: 0, theaterId: 1, integerCoordinates: { $map: { input: "$location.geo.coordinates", as: "coord", in: { $trunc: "$$coord" } } } } }, { $sort: { theaterId: 1 } } ] )
[ { theaterId: 1000, integerCoordinates: [ -93, 44 ] }, { theaterId: 1003, integerCoordinates: [ -76, 38 ] }, { theaterId: 1008, integerCoordinates: [ -121, 38 ] } ]
将算术运算符应用于每个数组元素
The following aggregation operation uses the $addFields stage to add a new genreScores field. The operation uses $map to apply $multiply and $add to each element in the genres array, computing a score based on the character count of each genre name:
db.movies.aggregate( [ { $match: { runtime: { $gt: 1000 } } }, { $addFields: { genreScores: { $map: { input: "$genres", as: "genre", in: { $add: [ { $multiply: [ { $strLenCP: "$$genre" }, 2 ] }, 1 ] } } } } }, { $project: { _id: 0, title: 1, genres: 1, genreScores: 1 } }, { $sort: { title: 1 } } ] )
[ { genres: [ 'Documentary', 'History', 'Sport' ], title: 'Baseball', genreScores: [ 23, 15, 11 ] }, { genres: [ 'Action', 'Adventure', 'Drama' ], title: 'Centennial', genreScores: [ 13, 19, 11 ] } ]
访问数组中每个项目的索引
movies集合中的 genres字段包含每部电影的类型名称数组。
以下示例使用 arrayIndexAs。myIndex 变量包含 genres数组中每种类型的索引。该示例返回包含这些document的字段:
电影标题。
类型名称。
类型在
rank字段的genres数组中的位置。isPrimary布尔值,数组中的第一个类型为true,其他类型为false。
db.movies.aggregate( [ { $match: { runtime: { $gt: 1000 } } }, { $project: { _id: 0, title: 1, rankedGenres: { $map: { input: "$genres", as: "genre", arrayIndexAs: "myIndex", in: { genre: "$$genre", rank: { $add: [ "$$myIndex", 1 ] }, isPrimary: { $eq: [ "$$myIndex", 0 ] } } } } } }, { $sort: { title: 1 } } ] )
[ { title: 'Baseball', rankedGenres: [ { genre: 'Documentary', rank: 1, isPrimary: true }, { genre: 'History', rank: 2, isPrimary: false }, { genre: 'Sport', rank: 3, isPrimary: false } ] }, { title: 'Centennial', rankedGenres: [ { genre: 'Action', rank: 1, isPrimary: true }, { genre: 'Adventure', rank: 2, isPrimary: false }, { genre: 'Drama', rank: 3, isPrimary: false } ] } ]
使用 $$IDX 访问索引
$$IDX 变量返回 input 数组中当前元素的索引。如果省略表达式中的 arrayIndexAs字段,则可以使用 $$IDX。
The following example returns the same documents as the previous example in Access the Index of Each Item in an Array, but uses $$IDX instead of arrayIndexAs:
db.movies.aggregate( [ { $match: { runtime: { $gt: 1000 } } }, { $project: { _id: 0, title: 1, rankedGenres: { $map: { input: "$genres", as: "genre", in: { genre: "$$genre", rank: { $add: [ "$$IDX", 1 ] }, isPrimary: { $eq: [ "$$IDX", 0 ] } } } } } }, { $sort: { title: 1 } } ] )
[ { title: 'Baseball', rankedGenres: [ { genre: 'Documentary', rank: 1, isPrimary: true }, { genre: 'History', rank: 2, isPrimary: false }, { genre: 'Sport', rank: 3, isPrimary: false } ] }, { title: 'Centennial', rankedGenres: [ { genre: 'Action', rank: 1, isPrimary: true }, { genre: 'Adventure', rank: 2, isPrimary: false }, { genre: 'Drama', rank: 3, isPrimary: false } ] } ]
了解详情
要了解上述示例中所用表达式的更多信息,请参阅: