Status: Needs Triage
Major - P3
The documentation on mongodump and mongorestore should clearly specify that at the exact place how quotes should be used (or should they not be used at all) while passing the values of --nsFrom and --nsTo arguments in the mongorestore command. While using single quotes (as written in the document) executes mongorestore properly in a Mac computer, but it fails in doing so in a Windows PC. To run the same in a Windows PC successfully, the values for --nsFrom and --nsTo need to be passed with either double quotes or no quotes at all. The correct procedure should be updated in the documentation properly.
In my Windows 10 PC using MongoDB 5.0.7 Community edition, while trying to copy an existing database to a new one using mongodump and mongorestore commands as mentioned in the documentation of MongoDB here, although mongodump is executing and creating the dump file properly, the mongorestore command fails every time generating a duplicate key error.
Surprisingly, it seems to restore the collection Products in mydb database instead of newdb. As mydb already contains the products collection having rows of documents with unique ids, it’s creating a clash generating the duplicate key error. Somehow, mongo seems to be ignoring the new database argument altogether.
- Create a database named mydb and create a collection called Products containing three documents in it.
# Dump the mydb database to an archive in the PC.
# Now restore the mydb database from the dump to a new database named newdb.
h3. Expected Results
As the mongodump command is executing and creating the dump file properly on the disc, the mongorestore command should have copied the contents of the mydb database into a new database named newdb.
Instead of copying the contents of mydb database to a new one named newdb, MongoDB is generating a duplicate key error.
While delving deep into the problem, I found the reason. The problem lies in using single quotes while passing the values of -
nsFrom and -nsTo arguments of the mongorestore command. While doing so, in some OS environments (especially Windows), mongo keeps on restoring the dump to the existing database instead of the new one, thus creating a clash between the existing data and the data being restored generating the duplicate key error. Instead, either using double quotes or not using any quote at all to pass the values of --nsFrom and --nsTo arguments solves the problem, thus executing mongorestore properly and copying the contents of mydb into newdb successfully.
Surprisingly, the mongorestore command works perfectly with single quotes while passing the values of --nsFrom and --nsTo arguments in a Mac computer, making it even more confusing for Windows users.
This seems to be a bug in MongoDB Server system or its syntax and needs to be rectified in the next update. The same needs to be updated in the documentation also. If there's some OS-specific syntax issue involved, the documentation on mongodump and mongorestore should clearly specify that at the exact place here |https://www.mongodb.com/docs/database-tools/mongodump/?&_ga=2.234397854.1469458796.1649914316-1657763159.1648793391#copy-clone-a-database].]
Otherwise, it ends up being extremely puzzling for readers, especially beginners, who would definitely try to implement the code in the documentation for testing and development.