KingFish
KingFish

Reputation: 9153

Aggregation / Group By w/ a Sum

I'm trying to get my head around the whole aggregation framework of Mongo. I'm kind of a newbie here.

 I have the following documents:
 { "col1": "camera", "fps": 1, "lat": 3 },
 { "col1": "camera", "fps": 3, "lat": 2 }
 { "col1": "recorders", "fps": 9, "lat": 7 }
 { "col1": "cell", "fps": 8, "lat": 1 }
 { "col1": "cell", "fps": 4, "lat": 3 }

How can I set something up to return the results as:

 { "col1": "camera", "fps": 4, "lat": 5 },
 { "col1": "recorders", "fps": 9, "lat": 7 },
 { "col1": "cell", "fps": 12, "lat": 4 },

Thanks

Upvotes: 2

Views: 106

Answers (2)

EmptyArsenal
EmptyArsenal

Reputation: 7464

I think this should do the trick:

db.collectionName.aggregate(
    { $group: { _id: "$col1", fps: { $sum: "$fps" }, lat: { $sum: "$lat" }}}
)

Upvotes: 1

Tom Swifty
Tom Swifty

Reputation: 2952

db.yourCollection.aggregate(
    { $group : {
        _id : "$col1",
        fps : { $sum : "$fps" },
        lat : { $sum : "$lat" }
    }}
);

Check this out!!! http://docs.mongodb.org/manual/reference/operator/aggregation/group/

Upvotes: 3

Related Questions