对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$map(表达式操作符)

$map

对数组中的每个项目应用表达式,并返回包含已应用结果的数组。

可以使用 $map 查找托管在以下环境中的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The $map expression has the following syntax:

{
$map: {
input: <expression>,
as: <string>,
arrayIndexAs: <string>,
in: <expression>
}
}
字段
规范

input

解析为数组的表达式。

如果 input 解析为 null 或指向缺失的字段,则 $map 返回 null。

如果 input 解析为非数组、非 null 值,则管道将出错。

as

可选。 代表 input 数组中每个元素的变量名称。 如果未指定名称,则变量名称默认为this。

arrayIndexAs

可选.聚合变量的名称,表示当前元素在 input 数组中的索引。第一个数组元素索引为 0。

您可以在表达式中使用变量名称。示例,如果指定 arrayIndexAs: "myIndex",则在表达式中使用 $$myIndex。$$myIndex 返回 input数组中当前元素的索引。

如果省略 arrayIndexAs,则可以在表达式中使用 $$IDX 系统变量来返回当前元素的索引。

For examples, see Access the Index of Each Item in an Array and Use $$IDX to Access the Index.

8.3版本新增。

in

应用于 input 数组中每个元素的表达式。 该表达式单独引用带有 as 中指定的变量名称的每个元素。

有关表达式的更多信息,请参阅表达式。

本页上的示例使用来自 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 变量返回 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 }
]
}
]

要了解上述示例中所用表达式的更多信息,请参阅: