1

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.

1 Answer 1

2

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]}
  }}
])
Sign up to request clarification or add additional context in comments.

1 Comment

Really smart way of solving the issue. Learned a lot just by reading this. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.