我的收藏有以下文件-

    {"_id":ObjectId("xxx"),"article_Id":"1","publish_Time":NumberLong(10)}
    {"_id":ObjectId("yyy"),"article_Id":"1","publish_Time":NumberLong(11)}
    {"_id":ObjectId("zzz"),"article_Id":"1","publish_Time":NumberLong(12)}


我想对article_id进行汇总,所需的结果有点像-

  {"_id":{"articleId":"1"}, pub_t:[publish_Time":NumberLong(10),publish_Time":NumberLong(11),publish_Time":NumberLong(10)]}


我想将单独文档中的publish_time字段合并为一个数组。
我怎样才能做到这一点?
提前致谢。

最佳答案

询问

db.articles.aggregate(
{
    $group:
    {
        _id:
        {
            articleId: "$article_Id"
        },
        pub_t:
        {
            $push: "$publish_Time"
        }
    }
})


结果

{
    "_id" : {
        "articleId" : "1"
    },
    "pub_t" : [
        NumberLong(10),
        NumberLong(11),
        NumberLong(12)
    ]
}

10-08 13:46