MongoDB – How to Rename All Databases When Doing a Mongorestore

mongodbmongodb-4.2mongodumpmongorestore

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?

Best Answer

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