E11000 duplicate key & mongorestore

mongorestore 可以将mongodump工具导出的数据导入到其他mongodb数据库中。 由于mongoDB中有_id字段总是作为主键存在,所以对于一般不会覆盖完全相同(包括_id)也相同的数据,因为如果导入的数据有重复的_id,那么会触发E11000 duplicate key错误。

 

例如

 

 

ac:~ maclean$ mongo
MongoDB shell version: 3.0.2
connecting to: test
> db.abc
db.abc
> db.duptest.insert({_id:10,x:10});
WriteResult({ "nInserted" : 1 })
> 
> 

ac:~ maclean$ mongodump -h localhost -p 27017  -d test -c duptest  --out duptest 
2015-05-11T22:22:36.743+0800	writing test.duptest to duptest/test/duptest.bson
2015-05-11T22:22:36.744+0800	writing test.duptest metadata to duptest/test/duptest.metadata.json
2015-05-11T22:22:36.745+0800	done dumping test.duptest

MongoDB shell version: 3.0.2
connecting to: test
> db.duptest.update({_id:10},{x:20});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.duptest.find();
{ "_id" : 10, "x" : 20 }
> 


ac:~ maclean$ mongorestore -h localhost -p 27017 duptest/
2015-05-11T22:23:32.355+0800	building a list of dbs and collections to restore from duptest/ dir
2015-05-11T22:23:32.356+0800	reading metadata file from duptest/test/duptest.metadata.json
2015-05-11T22:23:32.356+0800	restoring test.duptest from file duptest/test/duptest.bson
2015-05-11T22:23:32.357+0800	error: E11000 duplicate key error index: test.duptest.$_id_ dup key: { : 10.0 }
2015-05-11T22:23:32.357+0800	restoring indexes for collection test.duptest from metadata
2015-05-11T22:23:32.358+0800	finished restoring test.duptest
2015-05-11T22:23:32.358+0800	done
ac:~ maclean$ mongo
MongoDB shell version: 3.0.2
connecting to: test
> db.duptest.find();
{ "_id" : 10, "x" : 20 }

Comment

*

沪ICP备14014813号-2

沪公网安备 31010802001379号