如何从mongodb删除重复的行?

时间:2019-04-01 10:56:26

标签: python mongodb

  1. 如何在mongo中删除重复的行或文档?
    如何删除 与Python集成??

示例:

> db.US_Dollar.createIndex({currencies:1},{unique:1, dropDups:1})
{
        "ok" : 0,
        "errmsg" : "E11000 duplicate key error collection: shridhar.US_Dollar index: currencies_1 dup key: { : \"British Pound\" }",
        "code" : 11000,
        "codeName" : "DuplicateKey"
}
> db.US_Dollar.createIndex({bid:1},{unique:1, dropDups:1})
{
        "ok" : 0,
        "errmsg" : "E11000 duplicate key error collection: shridhar.US_Dollar index: bid_1 dup key: { : 0.9248 }",
        "code" : 11000,
        "codeName" : "DuplicateKey"
}
> db.US_Dollar.createIndex({sdk:1},{unique:1, dropDups:1})
{
        "ok" : 0,
        "errmsg" : "E11000 duplicate key error collection: shridhar.US_Dollar index: sdk_1 dup key: { : null }",
        "code" : 11000,
        "codeName" : "DuplicateKey"
}
> db.US_Dollar.createIndex({ask:1},{unique:1, dropDups:1})
{
        "ok" : 0,
        "errmsg" : "E11000 duplicate key error collection: shridhar.US_Dollar index: ask_1 dup key: { : 0.9204 }",
        "code" : 11000,
        "codeName" : "DuplicateKey"
}

1 个答案:

答案 0 :(得分:0)

根据您的唯一性标准创建唯一索引,并将dropDups设置为true

db.collection.createIndex({...}, {unique: true, dropDups: true})

了解如何使用PyMongo:How to delete duplicate rows from mongodb?