【问题标题】:Is there a way to eliminate all duplicates from a collection?有没有办法从集合中消除所有重复项?
【发布时间】:2020-06-23 18:42:11
【问题描述】:

我有一个集合,其中对象的结构类似于

{'_id': ObjectId('5e691cb9e73282f624362221'), 
 'created_at': 'Tue Mar 10 09:23:54 +0000 2020', 
 'id': 1237308186757120001, 
 'id_str': '1237308186757120001', 
 'full_text': 'See you in July'}

我正在努力只保留具有唯一全文的对象。使用 distinct 只会给我一个不同的全文字段值列表,因为我只想用唯一的全文保存集合中的对象。

【问题讨论】:

    标签: python python-3.x mongodb mongodb-query pymongo


    【解决方案1】:

    有,代码应该是这样的:

    dict = {"a": 1, "b": 2, "c": 3, "a": 5, "d": 4, "e": 5, "c": 8}
    
    #New clean dictionary
    unique = {}
    #Go through the original dictionary's items
    for key, value in dict.items():
        if(key in unique.keys()):
        #If the key already exists in the new dictionary
            continue
        else:
        #Otherwise
            unique[key] = value
    
    print(unique)
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      有两种方式:

      MongoDB方式

      我们执行 MongoDB 聚合,按full_text 对记录进行分组,仅过滤唯一文档并将它们插入集合中。 (在外壳中)

      db.collection.aggregate([
        {
          $group: {
            _id: "$full_text",
            data: {
              $push: "$$ROOT"
            },
            count: {
              $sum: 1
            }
          }
        },
        {
          $match: {
            count: {
              $eq: 1
            }
          }
        },
        {
          $addFields: {
            data: {
              $arrayElemAt: [
                "$data",
                0
              ]
            }
          }
        },
        {
          $replaceRoot: {
            newRoot: "$data"
          }
        },
        {
          $out: "tmp"
        }
      ])
      

      当您运行此查询时,它将创建具有唯一 full_text 值的新集合。您可以删除旧集合并重命名此集合。

      你也可以像{$out:"collection"}这样将你的收藏名称放入$out运算符中,但是没有回头路

      Python方式

      我们通过 full_text 字段执行 MongoDB 聚合分组,过滤重复文档并创建单个数组,并将所有 _id 删除。一旦 MongoDB 返回结果,我们对重复文档执行remove 命令。

      db.collection.aggregate([
        {
          $group: {
            _id: "$full_text",
            data: {
              $push: "$_id"
            },
            count: {
              $sum: 1
            }
          }
        },
        {
          $match: {
            count: {
              $gt: 1
            }
          }
        },
        {
          $group: {
            _id: null,
            data: {
              $push: "$data"
            }
          }
        },
        {
          $addFields: {
            data: {
              $reduce: {
                input: "$data",
                initialValue: [],
                in: {
                  $concatArrays: [
                    "$$value",
                    "$$this"
                  ]
                }
              }
            }
          }
        }
      ])
      

      MongoPlayground

      伪代码

      data = list(collection.aggregate(...))
      if len(data) > 0:
          colleciton.remove({'_id':{'$in':data[0]["data"]}})
      

      【讨论】:

      • 我不明白“Python 方式”的例子。它不是 pymongo 的有效聚合——它是什么?
      猜你喜欢
      • 1970-01-01
      • 2020-03-27
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      相关资源
      最近更新 更多