Q:

Get object at the root level mongodb

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}

A:

Use $unwind and $replaceRoot aggregation operators
db.collection.aggregate([   
  { "$unwind": "$sensors" },
  { "$replaceRoot": { "newRoot": { "$mergeObjects": ["$sensors", { "timestamp": "$timestamp" }] }}} 
])

