微信小程序
map
# AggregateCommand.map(value: any): Object
支持端:小程序 2.7.4, 云函数 0.8.1, Web
聚合操作符。类似 JavaScript Array 上的 map
方法,将给定数组的每个元素按给定转换方法转换后得出新的数组。
# 参数
# value: any
# 返回值
# Object
# API 说明
语法如下:
db.command.aggregate.map({
input: <expression>,
as: <string>,
in: <expression>
})
# 示例代码
假设集合 stats
有如下记录:
{
"_id": 1,
"sales": [ 1.32, 6.93, 2.48, 2.82, 5.74 ]
}
{
"_id": 2,
"sales": [ 2.97, 7.13, 1.58, 6.37, 3.69 ]
}
将各个数字截断为整形,然后求和
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
truncated: $.map({
input: '$sales',
as: 'num',
in: $.trunc('$$num'),
})
})
.project({
total: $.sum('$truncated')
})
.end()
返回结果如下:
{ "_id": 1, "index": 16 }
{ "_id": 2, "index": 19 }