Aggregation Unwind Document Keys as New Documents

Viewed 1377

I am having some problems in altering the schema I am using for a time series database I have constructed using Mongo DB. Currently, I have records like the one shown below:

{
    "_id" : 20,
    "name" : "Bob,
    "location" : "London",
    "01/01/1993" : {
         "height" : "110cm",
         "weight" : "60kg",
    },
   "02/01/1993" : {
         "height" : "112cm",
         "weight" : "61kg",
    }

}

I wish to use the aggregation framework to create several records for each "person", one for each "time-value" subdocument in the original record:

 {
    "_id" : 20,
    "name" : "Bob,
    "date" : "01/01/1993"
    "location" : "London",
    "height" : "110cm",
    "weight" : "60kg",
 },

 {
    "_id" : 20,
    "name" : "Bob,
    "date" : "02/01/1993"
    "location" : "London",
    "height" : "112cm",
    "weight" : "61kg",
 }

The new scheme should be much more efficient when adding a large number of time series values to each record and I shouldn't run into a max document size error!

Any help on how to do this using the Mongo DB aggregation pipeline would be greatly appreciated!

1 Answers
Related