mongodb mongorestore 命令替换现有记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27666908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongorestore command replace existing records?
提问by bertie
Is there a way for mongorestore to replace the records existing in the database instead of skipping it which is the default behavior ?
mongorestore 有没有办法替换数据库中存在的记录而不是跳过它,这是默认行为?
I'm currently using the latest 2.4.x version of mongodb.
我目前使用的是最新的 2.4.x 版本的 mongodb。
回答by vikas.sriv14
You could use the --drop
parameterof mongorestore
:
您可以使用该--drop
参数的mongorestore
:
Before restoring the collections from the dumped backup, drops the collections from the target database. --drop does not drop collections that are not in the backup.
在从转储备份还原集合之前,从目标数据库中删除集合。--drop 不会删除不在备份中的集合。
回答by wdberkeley
No. From mongorestore:
不。来自mongorestore:
If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.
如果还原到现有数据库,mongorestore 只会插入到现有数据库中,并且不会执行任何类型的更新。如果现有文档在目标数据库和集合中具有相同的 _id 字段值,则 mongorestore 不会覆盖这些文档。
If you want to completely overwrite the database or a collection within the database with mongorestore, drop the database or collection (check --dropparameter as pointed out by vikas's answer). If you want to replace certain documents or merge duplicate documents, then you'll need to write your own script to do the restore and implement the more complex logic yourself.
如果要使用 mongorestore 完全覆盖数据库或数据库中的集合,请删除数据库或集合(检查vikas的回答指出的--drop参数)。如果要替换某些文档或合并重复的文档,则需要编写自己的脚本来进行还原并自己实现更复杂的逻辑。
回答by TallerGhostWalt
If you have a small collection you could always:
如果你有一个小收藏,你总是可以:
- mongodump your target collection somewhere else
- delete your target collection
- mongorestore the original dump file
- mongorestore the dump file you made in step 1
- mongodump 你的目标集合在别处
- 删除您的目标集合
- mongorestore 原始转储文件
- mongorestore 您在步骤 1 中制作的转储文件
This switches the precedence to your dump file instead of to the _ids in the DB.
这会将优先级切换到您的转储文件而不是数据库中的 _ids。
回答by dynamo
I have solved this using
我已经解决了这个问题
- restore the collection and write the output to file (errors)
- 恢复集合并将输出写入文件(错误)
mongorestore --db database -c collection ~/path_to_bson > output.txt 2>&1
- filter out the lines containing
- E11000 duplicate key error collection ...ObjectId(...)
- then using multi-edit tool (in my case sublime) convert those lines to
db.collection.remove({_id: ObjectId(...)})
- restore the database again.
- 过滤掉包含的行
- E11000 duplicate key error collection ...ObjectId(...)
- 然后使用多编辑工具(在我的情况下是崇高的)将这些行转换为
db.collection.remove({_id: ObjectId(...)})
- 再次恢复数据库。