"goods": [
{
"_id": "e2297d935ec3342c00d9125250f266421f",
"num": 1
},
{
"_id": "37e26adb5ec39b120102ae453b7ad2921",
"num": 1
},
{
"_id": "37e26adb5ec39b120102ae453b7ad2921",
"gName": "可口可乐 300ml 12瓶装",
"price": 0.01,
"originalPrice": "80",
"description": "炎炎下入,快乐肥宅水等你来喝",
"classifyId": "aa9f906d5ec1f1b900fc5bdb2d680388",
"photos": [
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589872751534953931.png",
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589877511535131244.png",
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589871751535152226.png"
],
"_openid": "oKvsO0fqd22twirFCM12Tk1cVHnT0w",
"today": true
},
{
"_id": "e2297d935ec3342c00d915250f266421f",
"classifyId": "5e847ab25ec384be0142adf92b765f7d",
"photos": [
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589852117867166934.png",
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589285117867360109.png",
"cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589285117867441021.png"
],
"_openid": "oKvsO0fqd2t2wirFCM2Tk1cVHnT0w",
"gName": "山西苹果 丑的更好吃",
"price": 0.01,
"originalPrice": "16",
"description": "12月份最新鲜的苹果,只有30斤,卖了就没有了。",
"today": true
}
]
云开发,是否有办法,把重复的id对象合并,上面是4个结果,需要合并成2个结果
{ "group": "a", "name": "stu1", "score": 84 } { "group": "a", "name": "stu2", "score": 96 } { "group": "b", "name": "stu3", "score": 80 } { "group": "b", "name": "stu4", "score": 100 } const $ = db.command.aggregate db .collection('students') .aggregate() .group({ _id: '$group', students: $.push({ name: '$name', score: '$score' }) }) .end()
输出结果如下:
{ "_id": "b", "students": [{ "name": "stu3", "score": 80 }, { "name": "stu4", "score": 100 }] } { "_id": "a", "students": [{ "name": "stu1", "score": 84 }, { "name": "stu2", "score": 96 }] } 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/aggregate/AggregateCommand.push.html
db.collection('ddb_order')
.aggregate()
.lookup({
from: 'ddb_goods',
let: {
order_goodsList: '$goodsList',
},
pipeline: $.pipeline()
.match(_.expr($.and([
$.in(['$_id', '$$order_goodsList._id'])
])))
.project({
name: 1,
num: $.arrayElemAt(['$$order_goodsList.num', $.indexOfArray(['$$order_goodsList._id', '$_id'])]),
})
.done(),
as: 'bookList',
})
.end()
lodash库了解下