Get object at the root level mongodb
486
Use $unwind
and $replaceRoot
aggregation operators
db.collection.aggregate([
{ "$unwind": "$sensors" },
{ "$replaceRoot": { "newRoot": { "$mergeObjects": ["$sensors", { "timestamp": "$timestamp" }] }}}
])
Author by
Dmitry Sokolov
Updated on December 10, 2022Comments
-
Dmitry Sokolov over 1 year
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 about 5 yearsWhat should be the output?
-
Dmitry Sokolov about 5 yearsall documents from collection in format : timestamp, value0, value1, value2 {1555095522489, 0.153, -0.306, 9.807}
-
Ashh about 5 yearsPlease show the output in JSON format by editing your question
-
krishna Prasad about 5 yearsPossible duplicate of stackoverflow.com/questions/11189243/…
-