I have such a collection: Each document contains a field Message, which in turn contains an array Fields. Each document under the array Fieldshas properties Valueand Name.
[
{
"_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 searching for documents using, db.Collection.find({})I would like to design such that: he analyzes each Fieldunder Message.Fieldsand designs them in a new document, using Typeboth the name of the property and Valuethe value. The result will look like this:
[
{
"_id" : ObjectId("55711efed0103b1598140076"),
"Options" : 131,
"Length" : 8
},
{
"_id" : ObjectId("55711efed0103b1598140077"),
"Options" : 65,
"Length" : 13,
"Width" : 101
},
]
Is this achievable with function()or aggregateor in any other way in MongoDB?