Mongodb聚合 時間分組
阿新 • • 發佈:2019-01-08
查詢collection中,按天分組,每天的資料數量,並按照數量count倒序
// Requires official MongoShell 3.6+ use MediaNetDB; db.getCollection("TestCollection").aggregate( [ { "$project" : { "Day" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "$AddDateTime" } }, "Time" : { "$dateToString" : { "format" : "%H:%M:%S:%L", "date" : "$AddDateTime" } } } }, { "$group" : { "_id" : { "Day" : "$Day" }, "count" : { "$sum" : 1.0 } } }, { "$project" : { "_id" : 0.0, "Day" : "$_id.Day", "Count" : "$count" } }, { "$sort" : { "Count" : -1.0 } } ], { "allowDiskUse" : false } );