189 8069 5689

mongodb实现对对某列求和SUM

MongoDB数据如下:

目前创新互联已为数千家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器租用、企业网站设计、吉林网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

db.cpc_0103.find()

{ "a" : 8, "b" : "108177_200701010000", "c" : 108177, "d" : 180, "e" : 1404917, "f" : "U盘", "g" : 184543, "h" : "U盘网上批发市场", "i" : 90, "j" : 3, "k" : 3, "l" : "", "m" : "2007-01-01 00:00:00", "n" : "", "o" : "", "p" : -1, "q" : "--", "r" : 0, "s" : "" }

......

......

如何对其中的一列数据SUM,

xreport-cpc:PRIMARY> var reduce = function(key, values){

... ...     var count=0;

... ...     values.forEach(function(v) {

... ...        count+=v.p;

... ...     });

... ...     return {count: count};

... ... };

xreport-cpc:PRIMARY> var reduce = function(key, values){

... var count=0;

... values.forEach(function(v) {

... count+=v.cm;

... });

... return {count: count};

... };

xreport-cpc:PRIMARY> var s = db.c.find();

xreport-cpc:PRIMARY> var s = db.test.find();

xreport-cpc:PRIMARY> reduce("total_cm", s);

执行结果如下:

{ "count" : 20328860 }


当前文章:mongodb实现对对某列求和SUM
分享URL:http://cdxtjz.com/article/pcdpho.html

其他资讯