Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb frequently used command date, grouped

Shulou Source: shulou.com Published: 2022-06-01 09:52:38 11月12日 Update

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)

Tags: Grouping functions data queries normal objects documents tests commands dates parameters multiple arrays processing related federated commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology MySQL macOS OPPO Reno