重复索引的MongoDB错误

Posted

技术标签:

【中文标题】重复索引的MongoDB错误【英文标题】:Mongodb error for duplicate indexes 【发布时间】:2017-01-06 04:42:38 【问题描述】:

我是 MongoDB/Mongoose 的新手,并且正在转储我的 localdb 以便为 heroku 创建一个数据库。当我运行 mongorestore 时,出现以下错误:

2016-08-29T22:05:00.411-0500    building a list of collections to restore from /Users/micahsherman/tmp/mongodump/Loc8r dir
2016-08-29T22:05:00.518-0500    reading metadata for heroku_n1kxxxxxx.locations from /Users/micahsherman/tmp/mongodump/Loc8r/locations.metadata.json
2016-08-29T22:05:00.518-0500    reading metadata for heroku_n1kxxxxxx.test from /Users/micahsherman/tmp/mongodump/Loc8r/test.metadata.json
2016-08-29T22:05:00.519-0500    restoring heroku_n1kxxxxxx.test from /Users/micahsherman/tmp/mongodump/Loc8r/test.bson
2016-08-29T22:05:00.519-0500    restoring heroku_n1kxxxxxx.locations from /Users/micahsherman/tmp/mongodump/Loc8r/locations.bson
2016-08-29T22:05:00.520-0500    restoring indexes for collection heroku_n1kxxxxxx.test from metadata
2016-08-29T22:05:00.574-0500    finished restoring heroku_n1kxxxxxx.test (0 documents)
2016-08-29T22:05:00.799-0500    error: multiple errors in bulk operation:
  - E11000 duplicate key error index: heroku_n1kxxxxxx.locations.$_id_ dup key:  : ObjectId('57c334af05803d85c7b9e780') 
  - E11000 duplicate key error index: heroku_n1kxxxxxx.locations.$_id_ dup key:  : ObjectId('57c3819605803d85c7b9e783') 

2016-08-29T22:05:00.799-0500    restoring indexes for collection heroku_n1kxxxxxx.locations from metadata
2016-08-29T22:05:00.852-0500    finished restoring heroku_n1kxxxxxx.locations (2 documents)
2016-08-29T22:05:00.852-0500    done

这些会阻止来自 Heroku 的连接。

我已经进入控制台打印了索引,结果如下:

rs-ds017886:PRIMARY> db.system.indexes.find();
 "v" : 1, "key" :  "_id" : 1 , "name" : "_id_", "ns" : "heroku_n1kxxxxxx.test" 
 "v" : 1, "key" :  "_id" : 1 , "name" : "_id_", "ns" : "heroku_n1kxxxxxx.locations" 
 "v" : 1, "key" :  "coords" : "2dsphere" , "name" : "coords_2dsphere", "ns" : "heroku_n1kxxxxxx.locations", "background" : true, "2dsphereIndexVersion" : 2 
 "v" : 1, "key" :  "_id" : 1 , "name" : "_id_", "ns" : "heroku_n1kxxxxxx.objectlabs-system" 
 "v" : 1, "key" :  "_id" : 1 , "name" : "_id_", "ns" : "heroku_n1kxxxxxx.objectlabs-system.admin.collections" 
rs-ds017886:PRIMARY>

我不太确定如何进行调试。想法??

【问题讨论】:

【参考方案1】:

仔细检查您的locations.bson 文件以确保您没有重复的文档('57c334af05803d85c7b9e780''57c3819605803d85c7b9e783')。看起来您是在创建索引之前添加数据。

这是我认为正在发生的事情的一个例子(如果没有看到数据很难判断)

> db.people.insert(name: "Matt")

WriteResult( "nInserted" : 1 )

> db.people.insert(name: "Matt")

WriteResult( "nInserted" : 1 )

> db.people.createIndex(name: 1, unique: true)


    "ok" : 0,
    "errmsg" : "E11000 duplicate key error index: test.people.$name_1 dup key:  : \"Matt\" ",
    "code" : 11000

_id 字段已包含唯一约束

【讨论】:

以上是关于重复索引的MongoDB错误的主要内容,如果未能解决你的问题,请参考以下文章

mongoDB中的索引数组/子对象导致重复键错误

E11000 mongodb mongoose 中的重复键错误索引

E11000 mongodb mongoose 中的重复键错误索引

E11000 mongodb mongoose 中的重复键错误索引

如何跳过重复索引上的错误并继续在 MongoDB 中进一步添加文档(pymongo)

MongoDb在并非所有文档中的字段上创建唯一索引[重复]