从mongodb中删除重复的数组值

时间:2016-09-08 19:52:49

标签: arrays json mongodb

在mongodb中我有一个集合,其中数组有重复的条目,如

{
    "_id": ObjectId("57cf3cdd5f20a3b0ba009777"),
    "Chat": 6,
    "string": [
        "1348157031 Riyadh",
        " 548275320 Mohammad Sumon",
        " 1348157031 Riyadh",
        " 548275320 Mohammad Sumon",
        " 1348157031 Riyadh",
        " 1348157031 Riyadh"
    ]
}

我需要删除重复的数组并保留唯一的数组值,如下所示。

{
    "_id": ObjectId("57cf3cdd5f20a3b0ba009777"),
    "Chat": 6,
    "string": [
        "1348157031 Riyadh",
        " 548275320 Mohammad Sumon",
    ]
}

最好的方法是什么

感谢

2 个答案:

答案 0 :(得分:1)

db.getCollection('测试')。聚合([{$ unwind:' $ string'}, {$ group:{_ id:' $ _ id',string:{$ addToSet:' $ string'},聊天:{$ first:' $ Chat'} }}]);

O / P: 在这里,你得到2" 1348157031利雅得"因为有一个额外的空间将自己定义为一个不同的实体。

{
    "_id" : ObjectId("57cf3cdd5f20a3b0ba009777"),
    "string" : [ 
        " 1348157031 Riyadh", 
        " 548275320 Mohammad Sumon", 
        "1348157031 Riyadh"
    ],
    "Chat" : 6
}

答案 1 :(得分:0)

Mongo 3.4+具有$addFields聚合阶段,可让您避免显式列出要保留的所有其他字段:

collection.aggregate([
    {"$addFields": {
        "string": {"$setUnion": ["$string", []]}
    }}
])

仅供参考,这是另一种(更冗长的)方法,它使用$replaceRoot并且不需要列出所有可能的字段:

collection.aggregate([
    {'$unwind': {
        'path': '$string',
        // output the document even if its list of books is empty
        'preserveNullAndEmptyArrays': true
    }},
    {'$group': {
        '_id': '$_id',
        'string': {'$addToSet': '$string'},
        // arbitrary name that doesn't exist on any document
        '_other_fields': {'$first': '$$ROOT'},
    }},
    {
      // the field, in the resulting document, has the value from the last document merged for the field. (c) docs
      // so the new deduped array value will be used
      '$replaceRoot': {'newRoot': {'$mergeObjects': ['$_other_fields', "$$ROOT"]}}
    },
    {'$project': {'_other_fields': 0}}
])