Reputation: 2148
This is the sample collection:
{'f1': 10, '_id': 1, 'key': 'g', 'items': [{'i1': 10}, {'i1': 10}, {'i1': 10}]}
{'f1': 10, '_id': 2, 'key': 'g', 'items': [{'i1': 10}, {'i1': 10}, {'i1': 10}]}
{'f1': 77, '_id': 3, 'key': 'g', 'items': [{'i1': 10}, {'i1': 10}, {'i1': 10}]}
I want a formula like: $sum(f1 + Σ[items.i1])
to be computed on the above collection. Following is what I could come up with (in pymongo):
db.collec.aggregate([
{ "$unwind" : "$items"},
{ "$group" : {
"_id" : {"key": "$key", "id": "$_id"},
"matches" : { "$sum" : "$items.i1" },
"extra" : { "$sum" : "$f1" },
"count" : {"$sum": 1}
}},
{ "$group": {
"_id" : "$_id.key",
"finalSum":{ "$sum":
{ "$add": ["$matches", {"$divide":["$extra", "$count"]}]}}}}
]);
Output:
{'finalSum': 187.0, '_id': 'g'}
Although this gives correct output, I hope there's a better, simpler solution for this: Any help highly appreciated.
Upvotes: 2
Views: 1288
Reputation: 1056
When you are grouping the documents, you can save "f1" to the _id field, so that you don't need to summarize it and divide it for each document.
The aggregation operation is like this:
db.collec.aggregate([
{ "$unwind" : "$items"},
{ "$group" : {
_id : {key: "$key", id: "$_id", f1 : "$f1" },
matches : { "$sum" : "$items.i1" },
}},
{ "$group": {
_id : "$_id.key",
finalSum : { "$sum":
{ "$add": ["$matches", "$_id.f1"]}}}}
]);
Upvotes: 3