Monday, 15 February 2010

mongodb How to make age field in sum not count -



mongodb How to make age field in sum not count -

this question has reply here:

how aggregate sum in mongodb total count? 1 reply

if see, age field , using sum operator in mongodb same normal sql count operator. can please explain how utilize right sum in mongodb

> db.test.find() { "_id" : objectid("544654d01cdcaf6a66c188e8"), "age" : 23, "name" : "alex" } { "_id" : objectid("544654f01cdcaf6a66c188ec"), "age" : 33, "name" : "alex" } { "_id" : objectid("544654fb406947798c239544"), "age" : 14, "name" : "alex" } { "_id" : objectid("5446550a406947798c239547"), "age" : 25, "name" : "oleg" } { "_id" : objectid("54465523406947798c23954a"), "age" : 15, "name" : "oleg" } { "_id" : objectid("54465538406947798c23954e"), "age" : 10, "name" : "mark" } > db.test.aggregate([{$group:{_id:"$name","age":{$sum:1}}}]) { "_id" : "mark", "age" : **1** } { "_id" : "oleg", "age" : **2** } { "_id" : "alex", "age" : **3** }

you can write

db.test.aggregate([{$group:{_id:"$name","age":{$sum:"$age"}}}])

to sum expected.

the operand of $sum expression, sum made according final result of expression, added filed age group.

mongodb

No comments:

Post a Comment