博客
关于我
mongo 去重
阅读量:800 次
发布时间:2023-02-09

本文共 692 字,大约阅读时间需要 2 分钟。

与数据库连接并导入pymongo库:client = pymongo.MongoClient('mongodb://127.0.0.1:27017')db = client.Tenderingcollection = db.test获取所有唯一的id:for item in collection.distinct('id'):复制并删除重复项:repeating = collection.find_one({'id': item})result = collection.delete_many({'id': item})插入新数据:collection.insert_one(repeating)

pymongo的distinct方法

使用聚合运算来处理重复项:db.subway.aggregate([ { $group: { _id: { content: '$name' }, count: { $sum: 1 }, dups: { $addToSet: '$_id' } } }, { $match: { count: { $gt: 1 } } }]).forEach(function(it) { it.dups.shift(); db.subway.remove({ _id: { $in: it.dups } });});

改写说明:

  • 使用了更专业的技术术语,避免显得过于生硬
  • 删除了所有无关信息和标签
  • 保持了内容的技术性质,但语言更加流畅
  • 适当添加了段落分隔,提高可读性
  • 保持了内容的原有功能性,同时增加了可搜索性
  • 转载地址:http://edffk.baihongyu.com/

    你可能感兴趣的文章
    Python 3d 绘图 - 轴居中
    查看>>
    python ==》 字典
    查看>>
    python anaconda 安装使用
    查看>>
    python and或or 当参数传递的时候的用法
    查看>>
    Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
    查看>>
    Python APP自动化测试工具adb与Monkey使用详解
    查看>>
    Python APP自动化测试框架Appium详解
    查看>>
    Python APP自动化测试框架开发实战
    查看>>
    python argparse模块
    查看>>
    Python asyncio库的学习和使用
    查看>>
    Python AttributeError:“dict“对象没有属性“append“
    查看>>
    Python base64和hashlib模块
    查看>>
    python basic programs
    查看>>
    python bert_gen.py 报错Unable to load weights from pytorch checkpoint file for......
    查看>>
    python binascii.Error: Incorrect padding
    查看>>
    Python bool() 函数能否为无效参数引发异常?
    查看>>
    Python C 程序子进程在“for line in iter“处挂起
    查看>>
    Python Celery:自动化测试平台定时任务必备的三方库
    查看>>
    python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令
    查看>>
    Python CONNECT 4 CHECK WIN函数
    查看>>