Search code examples
mongodbmongorestore

Restoring from unknown database name to different database using --nsFrom and --nsTo


Since the deprecation of --db, I have been unable to restore an archive with an unknown database name to an existing database with a known name.

This is my attempt so far, which doesn't work, because I need to know the source database name: mongorestore --archive=/path/to/my.archive --nsFrom '[UNKNOWN DATABASE NAME].*' --nsTo '[KNOWN DATABASE NAME].*' --drop

If I use --nsFrom '*.*' --nsTo '[KNOWN DATABASE NAME].*', I get an error saying that the amount of wildcards has to match.


Solution

  • EDIT: The original answer below "works" but will restore the collections prefixed with original unknown database name.

    If you want the collections restored without the database name prefixed, use the $ method.

    mongorestore --archive=/path/to/my.archive --nsFrom '$prefix$.$suffix$' --nsTo '[KNOWN DATABASE NAME].$suffix$' --drop
    

    Original answer:

    The number of asterisks have to match in the --nsFrom and --nsTo arguments. If you don't know the name of the namespace used to create the archive, just use '*' which will map against all namespaces, e.g:

    mongorestore --archive=/path/to/my.archive --nsFrom '*' --nsTo '[KNOWN DATABASE NAME].*' --drop