7.13. MongoDB Database Tools
MongoDB Database Tools - a set of command line utilities for working with MongoDB databases. Available for Windows, macOS and Linux. A detailed description of all possibilities is available in official documentation.
Creating a dump
The dump is created using the utility mongodump
.
Key --gzip
responsible for archiving. With it, when creating a dump, the data of each collection will be saved in a separate archive in GZ format, without it, the data will not be archived and can be viewed with a regular editor.
Database
The command to dump the current contents of the database with all collections:
mongodump --authenticationDatabase=admin --gzip --db="database" --out="path/to/dump" -uuser -ppassword mongodb://example.mongo.tools:10000
In a team:
database
— the database for which you want to create a dump.path/to/dump
— path to the directory where the dump will be saved.user
- user login. Attention! The user must have role read or readWrite для данной or всех баз данных.password
- user password.mongodb://example.mongo.tools:10000
— connection string.
Restoring a dump
--gzip
.
The dump is restored using the utility mongorestore
.
Key --drop
is responsible for deleting collections. With it, before restoring, the collections that are in the dump will be deleted from the target database (other collections are not affected), without it, the data from the dump will be added to existing collections with the same names or ignored (if such data already exists in the collection), nothing will be deleted.
Database
The command to restore the database with all collections:
mongorestore --authenticationDatabase=admin --drop --gzip --dir="path/to/dump" -uuser -ppassword mongodb://example.mongo.tools:10000
In a team:
path/to/dump
— path to the directory with the database dump.user
- user login. Attention! The user must have role with access to the database and rights to modify it.password
- user password.mongodb://example.mongo.tools:10000
— connection string.
Collection
Command to restore a single database collection:
mongorestore --authenticationDatabase=admin --drop --gzip --dir="path/to/dump/database/collection.bson.gz" --db="database" -uuser -ppassword mongodb://example.mongo.tools:10000
In a team:
path/to/dump/database/collection.bson.gz
- path to the dump file of the collection.database
— the name of the database where you want to restore the data.user
- user login. Attention! The user must have role with access to the database and rights to modify it.password
- user password.mongodb://example.mongo.tools:10000
— connection string.