Thursday, 27 April 2017

simple sum with mongodb and nodejs

In MYSQL its simple to get sum of var. example:

SELECT SUM(money) FROM users

but now i moved to mongodb with nodejs and its very complex. According to inquiries that i made i fount that I must use a group method. But i didnt fount the answer to make that. In addition i didnt understand who to use the group method.

I have try to make this:

dbConnection.collection(collectionName).group({
    "initial": {
        "sum": 0
    },
    "reduce": function(obj, prev) {
        prev.sum = prev.sum + obj['money'] - 0;
    }
});

but without success.



via Razdom

No comments:

Post a Comment