For example, if I have a document like this
{ a: 1, subdoc: { b: 2, c: 3 } }
How can I convert it into a
You can use $replaceRoot with a $addFields stage as follows:
db.collection.aggregate([ { "$addFields": { "subdoc.a": "$a" } }, { "$replaceRoot": { "newRoot": "$subdoc" } } ])