在记录中具有值。
_id:asdgrsdv
surname:cooper,
comapany:sabesto,
salary:15748
mapped:Array
0:Object
name:'mark',
age:'25',
surname:'cooper'
1:Object
name:'snow',
age:'29',
surname:'wyte'
如何在mongodb聚合中将surname
与surname
中的mapped array
映射到外部
必需的输出:
_id:asdgrsdv
name:'mark',
comapany:sabesto,
salary:15748
age:'25',
surname:'cooper'
答案 0 :(得分:0)
尝试这个:
db.collection.aggregate([
{
$addFields: {
data: {
$mergeObjects: [
"$$ROOT",
{
$arrayElemAt: [
{
$filter: {
input: "$mapped",
cond: {
$eq: [
"$$this.surname",
"$surname"
]
}
}
},
0
]
}
]
}
}
},
{
$replaceRoot: {
newRoot: "$data"
}
},
{
$project: {
"mapped": 0
}
}
])