一个关于使用 $month,$dayOfMonth 进行按月/日统计的问题
生活随笔
收集整理的這篇文章主要介紹了
一个关于使用 $month,$dayOfMonth 进行按月/日统计的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個關于使用 $month,$dayOfMonth 進行按月/日統計的問題
情景如下:
按月,日進行統計分組:
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}
??????????? }}
]);
得到以下結果:
-----------------------------------------------------------------------------------------------------------------------------------------
{???? "_id" : {??????? "cmonth" : NumberInt(3),???????? "cday" : NumberInt(9)??? },???? "qty" : NumberInt(11)}
{???? "_id" : {??????? "cmonth" : NumberInt(3),???????? "cday" : NumberInt(10)??? },???? "qty" : NumberInt(14357)}
從上面可以看到,條件是 3/10 00:00:00 -3/11 00:00:00 統計的數據,但分組統計后出來了3/9號的數據。
但從下面統計:
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}
??????????? }}
]);
得到的結果如下:(兩次統計總量是對的)
-----------------------------------------------------------------------------------------------------------------------------------------
{?? "_id" : null,???? "qty" : NumberInt(14368)}
再做如下查詢:
db.test_tbl.find({"CreateTime":{$gte:ISODate("2016-03-10T00:00:00.000+0800"),$lt:ISODate("2016-03-10T08:00: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")}
可以看出,正好是11條,函數 $dayOfMonth 是基于 0時區來處理的,把0:00 -- 8:00 時間內的數據計算到了前一天了。
情景如下:
按月,日進行統計分組:
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}
??????????? }}
]);
得到以下結果:
-----------------------------------------------------------------------------------------------------------------------------------------
{???? "_id" : {??????? "cmonth" : NumberInt(3),???????? "cday" : NumberInt(9)??? },???? "qty" : NumberInt(11)}
{???? "_id" : {??????? "cmonth" : NumberInt(3),???????? "cday" : NumberInt(10)??? },???? "qty" : NumberInt(14357)}
從上面可以看到,條件是 3/10 00:00:00 -3/11 00:00:00 統計的數據,但分組統計后出來了3/9號的數據。
但從下面統計:
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}
??????????? }}
]);
得到的結果如下:(兩次統計總量是對的)
-----------------------------------------------------------------------------------------------------------------------------------------
{?? "_id" : null,???? "qty" : NumberInt(14368)}
再做如下查詢:
db.test_tbl.find({"CreateTime":{$gte:ISODate("2016-03-10T00:00:00.000+0800"),$lt:ISODate("2016-03-10T08:00: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")}
可以看出,正好是11條,函數 $dayOfMonth 是基于 0時區來處理的,把0:00 -- 8:00 時間內的數據計算到了前一天了。
所以在統計查詢時,條件與函數的時區要一致。比如上面的查詢條件不用時區就沒問題,如下:
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}
??????????? }}
]);
總結
以上是生活随笔為你收集整理的一个关于使用 $month,$dayOfMonth 进行按月/日统计的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB 增量备份方案
- 下一篇: MONGODB 2.6.8 的一个BUG