Rename all databases when doing a mongorestore?

All we need is an easy explanation of the problem, so here it is.

I have a datadump but I need to change the name of every database while restoring them,
if a database is named "customers" it needs to be "customer2", "products" to "products2"

I can rename an individual database but that will take a lot of time to rename each database separately

mongorestore --nsFrom='PetHotel.pets' --nsTo='PetHouse.pets' --nsInclude="PetHouse.*" dump/

how to I rename all database in a dump?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Should be this one:

mongorestore --nsInclude="PetHotel.*" --nsFrom='PetHotel.*' --nsTo='PetHouse.*' dump/

mongorestore --nsInclude='*.*' --nsFrom='$database$.*' --nsTo='$database$2.*' dump/

First test it with option --dryRun

See Change Collections’ Namespaces during Restore

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply