mongorestore command replace existing records?

前端 未结 4 1937
孤城傲影
孤城傲影 2021-01-01 10:29

Is there a way for mongorestore to replace the records existing in the database instead of skipping it which is the default behavior ?

I\'m currently using the lates

相关标签:
4条回答
  • 2021-01-01 10:42

    You could use the --drop parameter of 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.

    0 讨论(0)
  • 2021-01-01 10:42

    I have solved this using

    1. restore the collection and write the output to file (errors)
    mongorestore --db database -c collection ~/path_to_bson > output.txt 2>&1
    
    1. filter out the lines containing - E11000 duplicate key error collection ...ObjectId(...)
    2. then using multi-edit tool (in my case sublime) convert those lines to db.collection.remove({_id: ObjectId(...)})
    3. restore the database again.
    0 讨论(0)
  • 2021-01-01 10:50

    If you have a small collection you could always:

    1. mongodump your target collection somewhere else
    2. delete your target collection
    3. mongorestore the original dump file
    4. mongorestore the dump file you made in step 1

    This switches the precedence to your dump file instead of to the _ids in the DB.

    0 讨论(0)
  • 2021-01-01 10:52

    No. From 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.

    If you want to completely overwrite the database or a collection within the database with mongorestore, drop the database or collection (check --drop parameter 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.

    0 讨论(0)
提交回复
热议问题