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?

  • Are you using Linux or Windows? Don't know much about MongoDB but if you have a text dump, a bit of sed (and maybe awk) can go a long way (in Linux)! Apr 9 at 9:19
  • 1
    @Vérace, mongodumps are binary format, so could be difficult. Apr 9 at 10:11
  • @Vérace I am using WSL2 but I don't think i can use sed or awk to rename them, doesn't seem like a viable option
    – DB guy
    Apr 9 at 14:28

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.