1

document:

{"_id":"5cb0dfe234a8a30c9c0af127",
"sensors":
[{"value0":0.153,
"value1":-0.306,
"value2":9.807}],
"timestamp":1555095522489,"__v":0}

I want to get 4 field (timestamp and value 0..2) without any array / object. unwind work only against array but not objects. What should I do?

desired output :

{timestamp":1555095522489,
value0":0.153,
value1":-0.306,
value2":9.807}
Ashh
  • 41,249
  • 11
  • 91
  • 120
Dmitry Sokolov
  • 1,181
  • 12
  • 23

1 Answers1

4

Use $unwind and $replaceRoot aggregation operators

db.collection.aggregate([   
  { "$unwind": "$sensors" },
  { "$replaceRoot": { "newRoot": { "$mergeObjects": ["$sensors", { "timestamp": "$timestamp" }] }}} 
])
Ashh
  • 41,249
  • 11
  • 91
  • 120