文章目录
mongodb求和
Aggregation
1 2 3 4 5 6 7 8
| db.publicFoundation.aggregate([ { $match:{acceptDate:{$ne:null}} }, { $group:{'_id':null,"count":{'$sum':'$money'}}, } ]);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| DBObject match = new BasicDBObject(); DBObject acceptDate = new BasicDBObject(); acceptDate.put("$ne", null); match.put("acceptDate", acceptDate); DBObject db = new BasicDBObject(new BasicDBObject("$match", match)); DBObject object = new BasicDBObject(); DBObject group = new BasicDBObject(); object.put("_id", null); object.put("count", new BasicDBObject("$sum", "$money")); group.put("$group", object); AggregationOutput output = mongoTemplate.getCollection("publicFoundation").aggregate(db, group); Iterator iterator = output.results().iterator(); DBObject redult = null; int totalMoney = 0; while (iterator.hasNext()) { redult = (DBObject) iterator.next(); totalMoney = (int)redult.get("count"); }
|
db.mapReduce.insert([
{name:"jay",age:20},
{name:"mike",age:21}
]);
db.mapReduce.find();
db.mapReduce.mapReduce(
function(){
emit(this.age,this.age);
},
function(key,value){
return Array.sum(value)
},
{
//query:{age:},
out:"age"
}
);