0

I have time series data in a collection 'foo'. Each record has two fields - foodate and foovalue. What is the most efficient way to generate a n-period average, min and max of foovalue for every foodate?

1 Answer 1

0

Use the aggregation framework:

> db.foo.insert([{ date: 0, value: 0 }, { date: 0, value: 1 }, { date: 1, value: 2 }])
> db.foo.count()
3
> db.foo.aggregate(
    { $group: {
        _id: '$date',
        avg: { $avg: '$value' } }
    },
    { $project: {
        // Rename '_id' to 'date', include 'avg'.
        date: '$_id',
        _id: false,
        avg: true
    } },
    { $sort: { _id: 1 } })
2
  • @a-jesse-jiryu-davis Thanks. Also, is is possible to do a rolling n-period calc within mongo?
    – Vishal
    May 30, 2013 at 16:34
  • No; for that you should just query the data and do the calculation in your programming language. May 30, 2013 at 16:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.