I am trying to use MongoDB's aggregate-framework to generate documents from a collection which has the following structure:
{
"_id" : ObjectId("5510eb56f877bbef153d236d"),
"attributes" : {
"brand" : "Mercedes",
"price" : 289,
"family" : "GARDEN"
},
"name" : "Bigger Fix Soft Crisps"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236e"),
"attributes" : {
"brand" : "Adelholzener",
"price" : 683,
"family" : "COMPUTER"
},
"name" : "Soft Stockhome Chips"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236f"),
"attributes" : {
"brand" : "Pepsi",
"price" : 789,
"family" : "CAR"
},
"name" : "Light Yearsailfresh Meat"
}
I want to to get the aggregation of all fields from the attributes subdocument with their possible values. Since the fields in the subdocument are not known I cannot simply use the fields given in this example and therefore has to be dynamic. The result should look similar to this:
{
"_id" : "brand",
"values" : [
"Mercedes", "Adelholzener", "Pepsi"
]
},
{
"_id" : "price",
"values" : [
289, 683, 789
]
},
{
"_id" : "family",
"values" : [
"GARDEN", "COMPUTER", "CAR"
]
}
I haven't found a solution so far for this particularly problem. I tried with $project and $unwind (which does obviously only work for arrays).
brand,priceandfamily, other wise use map reduce