我是MongoDB的新手,我编写了一个聚合生成器,用于将每行中的两个嵌套数组合并在一起,然后将所有这些字段作为数组获取。
但是我有一个问题,那就是,嵌套数组中的每个对象都有一个对象ID,我想将它们作为结果。让我向您展示我所做的一个例子:
exporting: {
chartOptions: {
...
}
}
因此,结果将是:
db.customer.aggregate([
{
"$match": {
"withdraws.status": "PENDING"
}
},
{
"$sort": {
"withdraws.createdAt": -1
}
}, {
"$unwind": "$deposits"
},
{
"$unwind": "$withdraws"
}, {
"$project": {
"customer": {
"_id": "$_id",
"phone": "$phone",
"profile": "$profile"
},
"deposits": 1,
"withdraws": 1,
}
},
{
"$group": {
"_id": null,
"deposits": {
"$push": {
"$mergeObjects": [
"$deposits",
"$customer",
]
}
},
"withdraws": {
"$push": {
"$mergeObjects": [
"$withdraws",
"$customer"
]
}
}
}
}, {
"$project": {
"data": {
"$concatArrays": [
"$deposits",
"$withdraws"
]
}
}
}, {
"$unwind": {
"path": "$data",
"preserveNullAndEmptyArrays": true
}
},
{
"$replaceRoot": {
"newRoot": "$data"
}
}, {
"$skip": 0
},
{
"$limit": 2
},
]).toArray()
如您所见,所有结果中的_id属性与其他对象的_id值相同,而其数组中的每个存取对象都有唯一的_id。
有人可以帮我解决这个问题吗?