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 frequently used command date, grouped

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Date grouping db.msds_acce***ecord.group ({keyf: function (doc) {var date = new Date (doc.addtime); var dateKey = "+ date.getFullYear () +"-"+ (date.getMonth () + 1) +"-+ date.getDate (); return {'day':dateKey}; / / 33}, initial: {"count": 0}, reduce: function Reduce (doc, out) {if (doc.url) {out.count + = 1;})

Insert test data first:

For (var iTunes 1; i2)

}

});

Group federated $where query

Db.test.group ({key: {age:true}, initial: {num:0}, $reduce:function (doc,prev) {

Prev.num++

}

Condition: {$where:function () {

Return this.age > 2

}

}

});

3. Use function return value grouping

/ / Note that the function specified by $keyf must return an object

Db.test.group ({$keyf:function (doc) {return {age:doc.age};}, initial: {num:0}, $reduce:function (doc,prev) {

Prev.num++

}

});

Db.runCommand ({group:

{

Ns: "test"

$keyf:function (doc) {return {age:doc.age};}

Initial: {num:0}

$reduce:function (doc,prev) {

Prev.num++}

}

});

4. Use Terminator

Db.test.group ({$keyf:function (doc) {return {age:doc.age};}, initial: {num:0}, $reduce:function (doc,prev) {

Prev.num++

}

Finalize: function (doc) {doc.count=doc.num;delete doc.num;}

});

Db.runCommand ({group:

{

Ns: "test"

$keyf:function (doc) {return {age:doc.age};}

Initial: {num:0}

$reduce:function (doc,prev) {

Prev.num++}

Finalize: function (doc) {doc.count=doc.num;delete doc.num;}

}

});

About MapReduce

/ / insert test data first

For (var iTunes 1)

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