Import-Export based on Data Format
Note: Here we will be using uri as mentioned below as an example uri to mongodb database.
uri mongodb+srv://sandbox.pbsib.mongodb.net/myFirstDatabase
URI breakdown
Typical URI form:
srv: establishes a secure connection between an application and mongodb connection.
cluster URI: Unique Resource Identifier string.
Export or Import in JSON
mongoimport
Utility to import data into mongodb from a json file.
Example for mongodb atlas
mongoimport --uri="mongodb+srv:/<username>:<password>@<cluster>.mongodb.net/Databasename" --drop filename.json
Example for local machine
mongoimport --uri="mongodb://127.0.0.1:27017/Database_name" --drop filename.json
Note: In the above command, filename will be given as collection name
--drop
option, modifies the import process so that the target instance drops the collection before importing the data from the input, this avoids duplicate entries and any issues due to it.
mongoexport
Utility to export data from mongodb into a json file
Example for mongodb atlas
mongoexport --uri "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/Databasename" --collection=collection_name --out=filename.json
Example for local machine
mongoexport --uri="mongodb://127.0.0.1:27017/Database_name" --collection=collection_name --out=trial.json
Export or Import in BSON
mongorestore
Utility to import data into mongodb from a bson file
Example for mongodb atlas
mongorestore --uri "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/Databasename" --drop filename
Example for local machine
mongorestore --uri="mongodb://127.0.0.1:27017/FirstDatabase" --drop dump
dump is the folder which consist of collections of documents if collection is not specified
mongorestore --uri="mongodb://127.0.0.1:27017/FirstDatabase" --collection=collection_name --drop dump
mongodump
Utility to export data from mongodb into a bson file
Example for mongodb atlas
mongodump --uri "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/DatabaseName"
Returns the data in a folder called dump in pwd (present working directory)
Example for local machine
mongodump --uri="mongodb://127.0.0.1:27017/DatabaseName"
Last updated