MongoDB聚合:使用每日分组获取完整记录

时间:2017-05-26 08:56:32

标签: mongodb aggregation-framework

示例数据集

{
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-19T17:34:26.615Z"),
        "other_values" : "other"
}
{
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-19T12:34:26.615Z"),
        "other_values" : "other"
}
{
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-16T17:34:26.615Z"),
        "other_values" : "other"
}
{
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-16T17:34:26.615Z"),
        "other_values" : "other"
}

我想获取按日期分组的对象

{
_id : {day : 1}, [    {
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-19T17:34:26.615Z"),
        "other_values" : "other"
},
{
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-19T12:34:26.615Z"),
        "other_values" : "other"
}]
}
{
_id : {day : 2}, [    {
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-16T17:34:26.615Z"),
        "other_values" : "other"
},    {
        "_id" : ObjectId("591f0d3a69e95621e388604e"),
        "created_at" : ISODate("2017-05-16T17:34:26.615Z"),
        "other_values" : "other"
}

]     }

MongoDB的群组文档没有多大用处documentation我偶然发现了像this one这样的SO线程但却无法重新创建我想要的东西。

0 个答案:

没有答案