# 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:

    ```
    mongodb+srv://user:password@clusterURI.mongodb.net/database
    ```
* 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"
