日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个关于使用 $month,$dayOfMonth 进行按月/日统计的问题

發布時間:2024/4/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个关于使用 $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+0000"),$lt:ISODate("2016-03-11T00:00:00.000+0000")}
}},
??????????? {"$group":{
?? ??? ??? ??? ?"_id":{"cmonth":{"$month":"$CreateTime"},"cday":{"$dayOfMonth":"$CreateTime"}},
?? ??? ??? ??? ?qty:{$sum:1}
??????????? }}
]);






總結

以上是生活随笔為你收集整理的一个关于使用 $month,$dayOfMonth 进行按月/日统计的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。