假设每一条记录有一个叫做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);
结果如下:
国产著名网上商店系统,真正企业级应用软件,性能卓越,在国内外享有盛誉,用户遍布欧洲、美洲、大洋洲,支持多语言,前台与后台均可设置为不同语言界面,用户帮助文档极其丰富,PHP+MySQL+Zend运行环境,让你快速建立个性化的网上商店,内置几十种网上支付网关、内置数十套精美模板,支持实体、非实体商品销售。 更新功能调整: 1、应用中心:APP的“更新时间”字段
0
{ "count" : 600 }
Link to this post!
原文地址:在MongoDB中sum某个字段, 感谢原作者分享。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号