博客
关于我
mongo 去重
阅读量:797 次
发布时间: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/

    你可能感兴趣的文章
    Mysql 8.0 新特性
    查看>>
    MultCloud – 支持数据互传的网盘管理
    查看>>
    MySQL 8.0.23中复制架构从节点自动故障转移
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>