I have a data like this:
{
"_id" : 3623,
"identifier" : "sec_123",
"elements" : {
"title" : "elem_abc",
"sub_title" : "elem_def",
"media" : "elem_ghi",
"button" : "elem_jkl"
}
}
I want this to be modified like this:
"sec_123" : {
"title" : "elem_abc",
"sub_title" : "elem_def",
"media" : "elem_ghi",
"button" : "elem_jkl",
"give_us_call" : "elem_mno"
},
Is there a way to do this using MongoDb aggregation ?
CodePudding user response:
use $arrayToObject
db.collection.aggregate([
{
$replaceRoot: {
newRoot: {
$arrayToObject: [
[
{
k: "$identifier",
v: "$elements"
}
]
]
}
}
}
])