For example, if I have a document like this
{
a: 1,
subdoc: {
b: 2,
c: 3
}
}
How can I convert it into a
We can do this with $replaceWith an alias for $replaceRoot and the $mergeObjects operator.
let pipeline = [
{
"$replaceWith": {
"$mergeObjects": [ { "a": "$a" }, "$subdoc" ]
}
}
];
or
let pipeline = [
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [{ "a": "$a" }, "$subdoc" ]
}
}
}
];
db.collection.aggregate(pipeline)