示例:
> 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" }
答案 0 :(得分:0)
根据您的唯一性标准创建唯一索引,并将dropDups
设置为true
db.collection.createIndex({...}, {unique: true, dropDups: true})
了解如何使用PyMongo:How to delete duplicate rows from mongodb?