本文共 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/