I have a collection like this: Each document contains Message field which in turn contains array of Fields. Each document underneath Fields array has Value and Name properties.
[
{
"_id" : ObjectId("55711efed0103b1598140076"),
"Message" : {
"Fields" : [
{
"Value" : 131,
"Name" : "Options",
},
{
"Value" : 8,
"Name" : "Length",
}
]
}
},
{
"_id" : ObjectId("55711efed0103b1598140077"),
"Message" : {
"Fields" : [
{
"Value" : 65,
"Name" : "Options",
},
{
"Value" : 13,
"Name" : "Length",
},
{
"Value" : 101,
"Name" : "Width",
}
]
}
}
]
After finding documents using db.Collection.find({}), I would like to project such that - it parses each Field underneath Message.Fields and project them in new document using Type as property name and Value as value. The output would look like following:
[
{
"_id" : ObjectId("55711efed0103b1598140076"),
"Options" : 131,
"Length" : 8
},
{
"_id" : ObjectId("55711efed0103b1598140077"),
"Options" : 65,
"Length" : 13,
"Width" : 101
},
]
Is this achievable using function() or aggregate or any other way in MongoDB?