2

I have the following document structure:

{
    _id: ...,
    name: "Item1",
    Props: [
    {
        Key: "numberKey",
        Val: 1234
    },

    {
        Key: "dateKey",
        Val: Date("2013-09-09")
    }]
}

This is simplified and there can be various Keys and Values in Props field in the real application.

My question - is it possible to $group and $sum "numberKey"s by "dateKey"s?

What structure should I use if this is not possible? I need users to let add keys and values so I need something flexible.

1 Answer 1

2

Unfortunately, that isn't possible using aggregation with your schema. The problem is that aggregation is meant to operate over values in an array that are being selected by the $group clause and those elements have all the data needed. Your setup separates what you want to group by and what you want to sum. You could use a mapReduce job to do what you want with your schema. http://docs.mongodb.org/manual/core/map-reduce/ should be able to get you started.

Let me know if you have any other questions.

Best, Charlie

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I need this in the real-time so I'm not sure if mapReduce is the way. I decided to keep this schema and duplicate keys-values as fields for the aggregation purpose. I don't like this but it is probably the best way if I want to keep low count of indexes and be able to aggregate values at the same time.

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.