mongo聚合
db.getCollection('xxx').aggregate([
{$match:{"dt":"20191x10109","mediaId":101}},
{$group:{_id:null, count:{$sum:"$click"}}}
])
db.getCollection('xxx').aggregate(
[
{ $match : {"dt":"20191004", "customerId":"158892"} },
{ $group: { _id:"$customerId", "totalCost":{$sum: "$cost"} }}
]
)
map_reduce 聚合
db = client.get_database('xxx')
coll = db['material_video_data']
mapper = Code("""
function() { emit(this.fileMd5, this.cost);}
""")
reducer = Code("""
function(key, values) {
totalsum = 0;
for(i=0;i<values.length;i++){
totalsum+=parseFloat(values[i]);
}
return totalsum}
""")
momgo_result = coll.map_reduce(mapper, reducer, "myresult", query={"dt": self.dt},timeout=False)
操作MongoDB数据库,批量化地将某一属性的某一值更改为同一属性的另外一个值,如将status:2更改为status:0
>db.zgd_total_dataset.update({status:2},{$set:{status:0}}, false, true)