mongodb有能够sum的命令吗?
(创建时间:2012年09月07日 01:35:00)
Jangogo :
可以的
- 在sql中:select a,b,sum(c) csum from coll where active=1 group by a,b
在MongoDB中
- db.coll.group(
- {key: { a:true, b:true },
- cond: { active:1 },
- reduce: function(obj,prev) { prev.csum += obj.c; },
- initial: { csum: 0 }
- });
- key: Fields to group by.
- reduce: The reduce function aggregates (reduces) the objects iterated. Typical operations of a reduce function include summing and counting. reduce takes two arguments: the current document being iterated over and the aggregation counter object. In the example above, these arguments are named obj and prev.
- initial: initial value of the aggregation counter object.
- cond: An optional condition that must be true for a row to be considered. This is essentially a find() query expression object. If null, the reduce function will run against all rows in the collection.
db.coll.group( {key: { a:true, b:true }, cond: { active:1 }, reduce: function(obj,prev) { prev.csum += obj.c; }, initial: { csum: 0 } }); key: Fields to group by. reduce: The reduce function aggregates (reduces) the objects iterated. Typical operations of a reduce function include summing and counting. reduce takes two arguments: the current document being iterated over and the aggregation counter object. In the example above, these arguments are named obj and prev. initial: initial value of the aggregation counter object. cond: An optional condition that must be true for a row to be considered. This is essentially a find() query expression object. If null, the reduce function will run against all rows in the collection.
详细的你可以看官网
javascript:lnkchk('noitagerggA%2fSCOD%2fyalpsid%2fgro.bdognom.www%2f%2f%3aptth');
- db.coll.group(
- {key: { a:true, b:true },
- cond: { active:1 },
- reduce: function(obj,prev) { prev.csum += obj.c; },
- initial: { csum: 0 }
- });
- key: Fields to group by.
- reduce: The reduce function aggregates (reduces) the objects iterated. Typical operations of a reduce function include summing and counting. reduce takes two arguments: the current document being iterated over and the aggregation counter object. In the example above, these arguments are named obj and prev.
- initial: initial value of the aggregation counter object.
- cond: An optional condition that must be true for a row to be considered. This is essentially a find() query expression object. If null, the reduce function will run against all rows in the collection.
文档中心