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