Reputation: 351
I'm trying to set up a time series in mongodb (using this as a guide). Let's say that I have a collection where each document looks like this:
{
_id: '...',
timestamp_minute: '2018-01-01T12:30:00.000Z',
numbers: [[1, 2, ..., 10], ..., [1, 2, ..., 10]],
letters: [[a, b, ..., j], ..., [a, b, ..., j]]
}
How can I unwind it so it looks like this:
[
{_id: '...', timestamp_minute: '2018-01-01T12:30:00.000Z', values: 1, letters: a},
{_id: '...', timestamp_minute: '2018-01-01T12:30:00.000Z', values: 2, letters: b},
{_id: '...', timestamp_minute: '2018-01-01T12:30:00.000Z', values: 3, letters: c},
...
]
The data is stored on a document-per-minute basis with tenth-of-a-second precision. That means that the first level of values is usually length 60 (the seconds in a minute), and each nested one is length 10 (the tenth-of-a-second in a second).
I want to be able to unwind it in order to generate aggregated data.
Upvotes: 1
Views: 248
Reputation: 75984
You can try below aggregation in 3.4 version and more.
db.colname.aggregate([
{"$project":{
"timestamp_minute":1,
"numbersandletters":{
"$map":{
"input":{"$range":[0,{"$size":"$numbers"}]},
"as":"ix",
"in":{
"$zip":{
"inputs":[
{"$arrayElemAt":["$numbers","$$ix"]},
{"$arrayElemAt":["$letters","$$ix"]}
]
}
}
}
}
}},
{"$unwind":"$numbersandletters"},
{"$unwind":"$numbersandletters"},
{"$project":{
"timestamp_minute":1,
"values":{"$arrayElemAt":["$numbersandletters",0]},
"letters":{"$arrayElemAt":["$numbersandletters",1]}
}}
])
Upvotes: 2