Home > Database > Mysql Tutorial > 在MongoDB中sum某个字段

在MongoDB中sum某个字段

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:26:29
Original
2142 people have browsed it

假设每一条记录有一个叫做pv的整数字段,求整个集合中这个字段的和可以用如下方法: use testswitched to db test db.statistics.insertcreated_on: "2011-10-01", pv: 100 db.statistics.insertcreated_on: "2011-10-02", pv: 200 db.statistics.insertcreat

假设每一条记录有一个叫做pv的整数字段,求整个集合中这个字段的和可以用如下方法:

> use test
switched to db test
> db.statistics.insert({created_on: "2011-10-01", pv: 100})
> db.statistics.insert({created_on: "2011-10-02", pv: 200})
> db.statistics.insert({created_on: "2011-10-03", pv: 300})
> var reduce = function(key, values){
...     var count=0;
...     values.forEach(function(v) {
...        count+=v.pv;
...     });
...     return {count: count};
... };
> var s = db.statistics.find();
> reduce("total_pv", s);
Copy after login

结果如下:

{ "count" : 600 }
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template