Using MongoDB, I'm doing an aggregation pipeline on a document that contains an array called tests, and using an $unwind operation to unwind that array into several documents:
Original document:
{"_id"=>"1867930", "tests"=> [
{"status"=>"passed", "elapsed_time"=>26, "worker"=>11},
{"status"=>"passed", "elapsed_time"=>34, "worker"=>13},
{"status"=>"passed", "elapsed_time"=>23, "worker"=>15}
]}
Result after $unwind:
{"_id"=>"1867930", "tests"=>{"status"=>"passed", "elapsed_time"=>26, "worker"=>11}}
{"_id"=>"1867930", "tests"=>{"status"=>"passed", "elapsed_time"=>34, "worker"=>13}}
{"_id"=>"1867930", "tests"=>{"status"=>"passed", "elapsed_time"=>23, "worker"=>15}}
How can I apply another operation to hoist the values of the tests key in each of these documents up a level (i.e. eliminating the duplicate tests keys created from the $unwind) to get the following result?
{"_id"=>"1867930", "status"=>"passed", "elapsed_time"=>26, "worker"=>11}
{"_id"=>"1867930", "status"=>"passed", "elapsed_time"=>34, "worker"=>13}
{"_id"=>"1867930", "status"=>"passed", "elapsed_time"=>23, "worker"=>15}
Note that this is not renaming the tests key, it's moving the value of tests up a level and merging with the parent hash.
"tests.status"etc into just"status". That is what you are asking right? Just renaming keys. - Blakes Seventestsand merging it into the parent. - Ken Liu