I want to use the mongorestore
command in a script, but I am having troubles understanding exactly what kind of input it is looking for.
After using the mongodump
command, I end up with this tree:
mydirectory
└── dump
├── mydb1
│ ├── schemas.bson
│ └── schemas.metadata.json
├── mydb2
│ ├── schemas.bson
│ ├── schemas.metadata.json
│ ├── status.bson
│ └── status.metadata.json
└── mydb3
├── schemas.bson
└── schemas.metadata.json
I understood that I can use the mongorestore
command like this:
mydirectory$ mongorestore
since it is looking by default for the dump
directory.
However, I do not understand why using the following command:
mydirectory/dump$ mongorestore mydb1
give the following results:
2018-01-02T14:35:59.823+0100 building a list of dbs and collections to restore from mydb1 dir
2018-01-02T14:35:59.823+0100 don't know what to do with file "mydb1/schemas.bson", skipping...
2018-01-02T14:35:59.823+0100 don't know what to do with file "mydb1/schemas.metadata.json", skipping...
2018-01-02T14:35:59.823+0100 done
Moreover, when I use the -d
flag to specify a database to restore, it only works when I specify the directory in which this database is located, for example:
mydirectory/dump$ mongorestore mydb1 -d mydb1
(I would have expected this command to work without the -d
flag)
What kind of files or directory is mongorestore
expecting when using (or not) the -d
flag?
mongorestore
expects the dump
folder to contain sub-folders with the database name, which in turn contain the BSON dump and the metadata. The error you're seeing is because it didn't find any subdirectory with BSON/metadata files in it.
Rather than restoring by going into the dump
directory, it's better to use the --nsInclude
option instead (new in MongoDB 3.4). See the nsInclude documentation for more details.
The option --nsInclude
requires you to supply the namespace in the form of <database>.<collection>
. For example, to restore the test
database:
mongorestore --nsInclude "test.*"
To restore the test
collection inside the test
database:
mongorestore --nsInclude "test.test"
Make sure that you execute the restore from the dump
directory's parent, and not from inside it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With