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
  • 44,693
  • 14
  • 105
  • 132
Dmitry Sokolov
  • 1,303
  • 1
  • 17
  • 29

1 Answers1

4

Use $unwind and $replaceRoot aggregation operators

db.collection.aggregate([   
  { "$unwind": "$sensors" },
  { "$replaceRoot": { "newRoot": { "$mergeObjects": ["$sensors", { "timestamp": "$timestamp" }] }}} 
])
Ashh
  • 44,693
  • 14
  • 105
  • 132