Mongo按日期汇总总和和组项目

时间:2018-07-04 12:26:30

标签: mongodb aggregate

非常感谢您的宝贵时间。我正在一个集合中,我想对同一日期的项求和。考虑下面的示例,这里有两个文档,其中存储了user_id和played事件。我想对那些具有相同日期的文档进行汇总。就我而言,2017-01-25有两个结果,而2017-01-26只有一个结果。请调查预期的结果。

{
    "_id" : ObjectId("58891b5656a961427e7b23c6"),
    "user_id" : 122,
    "played_event" : [ 
        {
            "date" : ISODate("2017-01-25T21:43:48.146Z"),
            "totalPlayed" : 0,
        }, 
        {
            "date" : ISODate("2017-01-26T22:26:03.273Z"),
            "totalPlayed" : 838,
        }, 
    ]
}

{
    "_id" : ObjectId("58891b5656a961427e7b23f3"),
    "user_id" : 130,
    "played_event" : [ 
        {
            "date" : ISODate("2017-01-25T21:43:48.146Z"),
            "totalPlayed" : 0,
        }, 
        {
            "date" : ISODate("2017-01-30T22:26:03.273Z"),
            "totalPlayed" : 838,
        }, 
    ]
}
  

预期结果

{
    "result" : [ 
        {
            "date" : "2017-01-25"
            "sum" : 2
        }, 
        {
            "date":"2017-01-26"
            "sum":1
        }, 
    ],
    "ok" : 1
}

我正在尝试使用以下代码

[{"$unwind":"$played_event"},{"$match":{"$and":[{"played_event.date":{"$lte":{"sec":1530766799,"usec":0},"$gte":{"sec":1530162000,"usec":0}}},{"game_id":1}]}},{"$match":{"user_id":{"$nin":[1,2]}}},{"$group":{"_id":"$user_id","total":{"$sum":"$played_event.totalPlayed"},"events":{"$push":"$played_event.date"}}},{"$project":{"_id":0,"user_id":"$_id","total":1,"events":1}}]

但是它没有给我预期的结果,我在查询中总结了totalPlayed,但这不是必需的。

3 个答案:

答案 0 :(得分:2)

您需要首先$unwind "played_event",然后需要$group,方法是使用$dateToString"date"放置所需的格式

db.collection.aggregate([
  { "$unwind": "$played_event" },
  { "$group": {
    "_id": {
      "$dateToString": {
        "format": "%Y-%m-%d",
        "date": "$played_event.date"
      }
    },
    "sum": { "$sum": 1 }
  }},
  { "$project": {
    "date": "$_id",
    "sum": 1,
    "_id": 0
  }}
])

输出

[
  {
    "date": "2017-01-30",
    "sum": 1
  },
  {
    "date": "2017-01-26",
    "sum": 1
  },
  {
    "date": "2017-01-25",
    "sum": 2
  }
]

答案 1 :(得分:1)

{
    'unwind' : '$played_event'
},
{
    '$group' : {
        _id : { $concat: [ { $year: "$date" }, "+", { $month: "$date" }, "+", { $dayOfMonth: "$date" }] }

        "sum" : { $sum : 1}
    }, 
},
{
    $match : {
        _id : { $in : ["2017-01-25", "2017-01-26"] }
    }
},
{
    $project : { _id : 0, "date" : "$_id", "sum" : 1
}

答案 2 :(得分:1)

另一种实现此目的的方法。此解决方案是我首选的解决方案,因为它更容易在2个任意日期之间进行计数,而不是按日期/月/年

db.test2.aggregate([
        {
            $unwind: {path : "$played_event"}
        },
        {
            $project: { day: { $dateToString: { format: '%Y-%m-%d', date: '$played_event.date' } } }
        },
        {
            $bucket: {
                groupBy: "$day",
                boundaries: [ "2017-01-25","2017-01-26","2017-01-27" ], //bucket is inclusive for start, exclusive for end
                default: "other",
                output: { count: { $sum: 1 } }
            }
        },
    ]
);

输出:

 { 
    "_id" : "2017-01-25", 
    "count" : 2.0
}
{ 
    "_id" : "2017-01-26", 
    "count" : 1.0
}
{ 
    "_id" : "other", 
    "count" : 1.0
}