Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb implements the SUM of the sum of a column

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The mongodb data are as follows:

Db.cpc_0103.find ()

{"a": 8, "b": "108177 200701010000", "c": 108177, "d": 180," e ": 1404917," f ":" U disk "," g ": 184543," h ":" U disk online wholesale market "," I ": 90," j ": 3," k ": 3," l ":" "m": "2007-01-01 00:00:00", "n": "," o ":", "p":-1, "Q": "-", "r": 0, "s": ""}

.

.

How to SUM one column of data

Xreport-cpc:PRIMARY > var reduce = function (key, values) {

...... Var count=0

...... Values.forEach (function (v) {

...... Count+=v.p

.})

...... Return {count: count}

......}

Xreport-cpc:PRIMARY > var reduce = function (key, values) {

... Var count=0

... Values.forEach (function (v) {

... Count+=v.cm

.)

... Return {count: count}

...}

Xreport-cpc:PRIMARY > var s = db.c.find ()

Xreport-cpc:PRIMARY > var s = db.test.find ()

Xreport-cpc:PRIMARY > reduce ("total_cm", s)

The implementation results are as follows:

{"count": 20328860}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report