How to fix dup key error during mongorestore

I have a problem restoring data using mongorestore:
First, I used this command to export the data and everything seems to work fine:
mongodump -h 127.0.0.1 -d backup -o ~/
Then, I executed this command many times to restore the data:
mongorestore -h 127.0.0.1 -d backup -dir ~/backup/
But got this error:

2023-07-21T06:05:16.168-0400    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2023-07-21T06:05:16.168-0400    building a list of collections to restore from /home/zdx/backup dir
2023-07-21T06:05:16.169-0400    **reading metadata for ir.backup from** /home/zdx/backup/backup.metadata.json
2023-07-21T06:05:16.170-0400    **restoring to existing collection ir.backup without dropping**
2023-07-21T06:05:16.170-0400    restoring ir.backup from /home/zdx/backup/backup.bson
2023-07-21T06:05:16.181-0400    continuing through error: E11000 duplicate key error collection: ir.backup index: _id_ dup key: { _id: ObjectId('64b5a5865e23e338c4fdfe27') }
2023-07-21T06:05:16.181-0400    continuing through error: E11000 duplicate key error collection: ir.backup index: _id_ dup key: { _id: ObjectId('64b5a643a374f41eff1c897e') }
2023-07-21T06:05:16.181-0400    finished restoring ir.backup (0 documents, 2 failures)
2023-07-21T06:05:16.181-0400    no indexes to restore for collection ir.backup
2023-07-21T06:05:16.181-0400    0 document(s) restored successfully. 2 document(s) failed to restore.

My problem is, I have specified the “backup” database, but the data is restored to the “ir” database, why?

Operating environment:
OS version: CentOS Linux release 7.2.1511 (Core)
Mongodb :

Current Mongosh Log ID: 64ba59fb42ffe11856bda7cb
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB:          6.0.8
Using Mongosh:          1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-07-21T05:52:46.578-04:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2023-07-21T05:52:46.579-04:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-07-21T05:52:46.579-04:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
   2023-07-21T05:52:46.579-04:00: vm.max_map_count is too low

Hi @Avalon_Zhou and welcome to MongoDB community forums!!

Firstly, thank you for recording this behaviour and reporting the error. I tried to reproduce the error using the command mentioned, and I am seeing the similar behaviour. The reason for that is the -dir used in the mongorestore command, is treated as -d ir which overwrites the database name as ir.
This has been reported internally and you can track any progress / updates with the following ticket TOOLS:3357.
One possible workaround here is to use the restore command as:
mongorestore -h 127.0.0.1 -d backup --dir ~/test/ which would restore as:
reading metadata for backup.sample from ........

Please test this workaround on a test environment to see if it works for you before proceeding to do it on a production environment.

Further, regarding the duplicate key error seen in the above logs is because mongorestore would only perform the insert and would not perform any update operations. Please refer to the documentation on mongorestore Inserts Only for more information

You can refer to the response by @Prasad_Saya, our community user where he has described the same with a very efficient example.

Let us know of you have further questions.

Regards
Aasawari

1 Like

Thanks for your reply. This little problem has been bugging me all day. But luckily it was resolved in the end. :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.