Explanation: How we can make a duplication of the field f1
. I want to introduce the replica of the object f1
that name as f2
, see the expected output
{
"data": [
{
"value": 100,
"fl" :1
},
{
"value": 300,
"fl" :0
},
{
"value": 14,
"fl" :0
},
{
"value": 3
"fl" :0
},
{
"value": 30,
"fl" :1
},
{
"value": 60,
"fl" :1
}
]
}
Expected output
{
"data": [
{
"value": 100,
"fl" :1,
"f2" :1
},
{
"value": 300,
"fl" :0,
"f2" :0
},
{
"value": 14,
"fl" :0,
"f2" :0
},
{
"value": 3,
"fl" :0,
"f2" :0
},
{
"value": 30,
"fl" :1,
"f2" :1
},
{
"value": 60,
"fl" :1,
"f2" :1,
}
]
}
CodePudding user response:
Query
- its similar with the previous here without condition, read
$map
documentation, you will be able to do all those
db.collection.aggregate([
{
"$set": {
"data": {
"$map": {
"input": "$data",
"in": {
"$mergeObjects": [
"$$m",
{
"f2": "$$m.fl"
}
]
},
"as": "m"
}
}
}
}
])