I believe that the mongodb aggregation structure is extremely efficient - it seems like a good opportunity to smooth out an object. My schema uses an array of helper objects in an array called materials. The number of materials is variable, but a specific field (category) will be unique to objects in the array. I would like to use the aggregation structure to smooth the structure and dynamically rename the fields based on the value of the category field. I could not find an easy way to accomplish this using $ project along with $ cond. Is there any way?
The reason for the array of material objects is a simple search:
eg. {'materials.name': 'XYZ'} discards any document in which XYZ is found.
eg. before and after the document
{
"_id" : ObjectId("123456"),
"materials" : [
{
"name" : "XYZ",
"type" : "Red",
...
"category" : "A"
},
{
"name" : "ZYX",
"type" : "Blue",
...
"category" : "B"
}]
}
{
"material_A_name" : "XYZ",
"material_A_type" : "Red",
...
"material_B_name" : "ZYX",
"material_B_type" : "Blue",
...
}