Need advice on how to sum a field in a Collection


#1

I may be over-thinking this after a lot of web searching because a really trivial task is starting to look ugly.

How do I get the sum of a field from a collection? This is boring, accounting kind of stuff. Given:
Ledger = new Mongo.Collection(‘ledger’);
// userId:
// date:
// account: Cash, inventory etc
// amount; positive for debits / negative for credits
// comment: instead of a journal

how do I get, for instance, the sum of records for the Cash account?

I came across meteorhacks:aggregate. The example is a bit spare for my nascent grasp of Mongo, I’m afraid. My attempts to use it are failing.

But it seems like this shouldn’t be this hard to do. What am I missing?

Thanks


#2

This is the answer given to me in stockoverflow. http://stackoverflow.com/questions/24729768/meteor-group-collection-by-field-and-their-sum

Hope this helps as they can be done in server or in the client


#3

ralpheiligan,
Thank you for replying. That solution is more complex than I’m asking for. You needed sums by category - I’m just looking to get the sum of the ‘found’ records.

Plus it’s a static solution. Which would be appropriate for a report of some sort but not viable in a dynamic situation.

I am obviously new to Meteor. As a learning exercise I’m building a little market based game. Meteor is great for managing the mechanics in most ways but this is a real shortcoming.

My other option is to structure things so some key values like Cash are maintained from the transactions. Like adding a ‘cash’ field to the user profile I wouldn’t actually use the user profile, of course) and adding and subtracting each transaction. This is ok for a game but in real life I’d want to be able to calc such a number from the actual records. Easily.


#4

I haven’t done this, but when I saw your question I did some searching thinking that I might too have to do this at some point. I found this SO thread:

Have you tried this, $unwind sounds interesting…


#5

cstrat,
Thanks for the lead. A couple of things come up - this is addressing the mongo db directly. Can I do that within Meteor code? And is .aggregate a Mongo function or a package?

The $unwind trick is good to know about. In my case, though, I’m not trying to get the sum of embedded elements but simply the sum of a first level field in the documents themselves. In the nomenclature of the stackflow example I’d have a few records and want the sum of ‘Monto’ field at the top level.

Coming from the relational db world this is bringing me hard up against a fundamental difference in Mongo.

How do you work around these sorts of things?


#6

Edit: I’ve looked into your document more carefully, it seems you’re trying to sum over a specific type of embedded document (Cash)… I think this might actually be of more use.

$group is what you want. If you’ve changed your mind on using aggregate, there’s a involved example at this link which has exactly what you are looking for. Scroll down to the “Group by null” section, in the middle.

http://docs.mongodb.org/manual/reference/operator/aggregation/group/

Using meteorhacks:aggregate, All you’d have to do is

Legder.aggregate([
  {$group: ...} //the example in the link shows you what goes in there.
])