如何从mongoDB中删除重复项并使用聚合函数保留唯一数据

时间:2018-01-28 06:36:33

标签: mongodb mongodb-query

您好我一直试图在Mongodb中显示没有重复项的唯一数据输出。

以下是我的mongoDB中整个文档的一个示例

{
        "_id" : ObjectId("5a43aa19d4b45e362428e2da"),
        "comments_data" : {
                "id" : "28011986676_10155780942281677",
                "comments" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "WTI5dGJXVnVkRjlqZAFhKemIzSTZAN4TlRBeE5EWXlPQT09",
                                        "before" : "WTI5dGJXVnVk4TVRZAMk56YzZANVFV4TlRBeE5EWXlPQT09"
                                }
                        },
                        "data" : [
                                {
                                        "created_time" : "2018-01-03T21:23:47+0000",
                                        "message" : "Poor customer care service after became the Singtel customer.I did my          re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service.",
                                        "from" : {
                                                "name" : "Sundararaju G",
                                                "id" : "1020391"
                                        },
                                        "id" : "10155780942281677_10155811924116677"
                                }
                        ]
                }
        },
        "post_id" : "28011986676_10155780942281677",
        "post_message" : "\"Singtel TV celebrated our 10th birthday with 10 awesome experiences for our customers! Each of our winners won a trip of a lifetime - from attending the Emmy Awards, getting a magical princess treatment at Disneyland, to catching a Premier League game live in London! We thank all our customers for your support and we look forward to more great years to come!\"",
        "reactions_data" : {
                "reactions" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "TVRBd01EQXpNVEF5T1Rje4TXc9PQZDZD",
                                        "before" : "TVRjNE56TTBBek56a3hNek14TWc9PQZDZD"
                                },
                                "next" : "https://graph.facebook.com/v2.7/280119866761677/reactions?access_token=EAA"
                        },
                        "data" : [
                                {
                                        "type" : "ANGRY",
                                        "id" : "1020573391",
                                        "name" : "Sundararaju Gh"
                                },
                                {
                                        "type" : "LIKE",
                                        "id" : "64721496",
                                        "name" : "Zhiang Xian"
                                }
                        ]
                },
                "id" : "28011986676_102281677"
        }
}

之后我一直试图在comments_data字段中提取消息评论。 我尝试过使用此查询

db.sInsert.find({post_id: "28011986676"}, {post_id:1, 'comments_data.comments.data.message':1})

我得到的结果是所有重复数据。

{ "_id" : ObjectId("5a43aa19d4b45e362428e2ec"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
{ "_id" : ObjectId("5a4660f2d4b45e3698398320"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
{ "_id" : ObjectId("5a47ae92d4b45e2148941901"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
{ "_id" : ObjectId("5a4928b1d4b45e208cfd6916"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }

此后,我尝试在MongoDB中使用聚合函数

db.sInsert.aggregate([   { $match: {post_id: {"$eq": "28011986676" } } },   { $project: {'message': '$comments_data.comments.data.message', _id:0} }, ])

我得到的结果是

{ "message" : [ "Who else loves Apple ?" ] }
{ "message" : [ "Who else loves Apple ?" ] }
{ "message" : [ "Who else loves Apple ?" ] }
{ "message" : [ "Who else loves Apple ?" ] }

如何使用聚合函数获得唯一结果?我也尝试过使用ensureIndex,它仍然没有帮助从mongoDB返回唯一的结果。

1 个答案:

答案 0 :(得分:0)

如果您只需要不同的消息,则只需执行不同的查询

即可
db.sInsert.distinct(
    "comments_data.comments.data.message", 
    {post_id: {"$eq": "28011986676" }}
)

您也可以使用$group$addToSet进行聚合以获取相同的消息

db.sInsert.aggregate(
    {$match :  {post_id: {"$eq": "28011986676" }}},
    {$group : {_id:null, messages: {$addToSet : "$comments_data.comments.data.message"}}},
    {$project : {_id:0, messages: {$arrayElemAt : ["$messages", 0]}}}
)