A question on the use of $month, $dayOfMonth monthly/daily Statistics
The scenario is as follows:
Grouped by month and day:
Db.test_tbl.aggregate ([
{"$match": {"Createtime": {$gte: Isodate ("2016-03-10t00:00:00.000+0800"), $lt: Isodate ("2016-03-11t00:00:00.000+ 0800 ")}
}},
{"$group": {
"_id": {"Cmonth": {"$month": "$CreateTime"}, "Cday": {"$dayOfMonth": "$CreateTime"}},
qty:{$sum: 1}
}}
]);
The following results are obtained:
--------------------------------------------------------------------------------------------------------------- --------------------------
{"_id": {"Cmonth": Numberint (3), "Cday": Numberint (9)}, "Qty": Numberint (11)}
{"_id": {"Cmonth": Numberint (3), "Cday": Numberint (A)}, "Qty": Numberint (14357)}
As can be seen from above, the condition is 3/10 00:00:00-3/11 00:00:00 statistic data, but after the grouping statistics comes out 3/9 number data.
But from the following statistics:
Db.test_tbl.aggregate ([
{"$match": {"Createtime": {$gte: Isodate ("2016-03-10t00:00:00.000+0800"), $lt: Isodate ("2016-03-11t00:00:00.000+ 0800 ")}
}},
{"$group": {
"_id": null,
qty:{$sum: 1}
}}
]);
The results are as follows: (two total statistics are right)
--------------------------------------------------------------------------------------------------------------- --------------------------
{"_id": null, "qty": Numberint (14368)}
And then do the following query:
Db.test_tbl.find ({"Createtime": {$gte: Isodate ("2016-03-10t00:00:00.000+0800"), $lt: Isodate ("2016-03-10t08:0 0:00.000+0800 ")}},{createtime:1}). Sort ({createtime:-1});
--------------------------------------------------------------------------------------------------------------- --------------------------
{"_id": Numberlong (258579), "Createtime": Isodate ("2016-03-10t07:42:56.063+0800")}
{"_id": Numberlong (258578), "Createtime": Isodate ("2016-03-10t06:53:11.203+0800")}
{"_id": Numberlong (258577), "Createtime": Isodate ("2016-03-10t06:53:11.156+0800")}
{"_id": Numberlong (258576), "Createtime": Isodate ("2016-03-10t06:53:11.125+0800")}
{"_id": Numberlong (258575), "Createtime": Isodate ("2016-03-10t06:53:11.094+0800")}
{"_id": Numberlong (258574), "Createtime": Isodate ("2016-03-10t06:53:11.078+0800")}
{"_id": Numberlong (258573), "Createtime": Isodate ("2016-03-10t06:53:11.063+0800")}
{"_id": Numberlong (258572), "Createtime": Isodate ("2016-03-10t06:53:10.953+0800")}
{"_id": Numberlong (258571), "Createtime": Isodate ("2016-03-10t06:53:10.938+0800")}
{"_id": Numberlong (258570), "Createtime": Isodate ("2016-03-10t06:53:10.906+0800")}
{"_id": Numberlong (258569), "Createtime": Isodate ("2016-03-10t06:53:10.438+0800")}
As you can see, exactly 11, the function $dayOfMonth is based on the 0 time zone, the 0:00-8:00 time to calculate the data to the day before.
So when you're counting queries, the conditions are consistent with the time zone of the function. For example, the above query conditions do not use the time zone is not a problem, as follows:
Db.test_tbl.aggregate ([
{"$match": {"Createtime": {$gte: Isodate ("2016-03-10t00:00:00.000+0000"), $lt: Isodate ("2016-03-11t00:00:00.000+ 0000 ")}
}},
{"$group": {
"_id": {"Cmonth": {"$month": "$CreateTime"}, "Cday": {"$dayOfMonth": "$CreateTime"}},
qty:{$sum: 1}
}}
]);