Web Applications Development

Thursday, July 3, 2025

Export and Import Data from MongoDB

Export Data from MongoDB

To export data from a MongoDB database using MongoDB Tools, particularly mongodump and mongoexport, follow the appropriate method based on your use case:

✅ Option 1: Use mongoexport

Exports collection data in JSON or CSV format.

🔹 Example: Export a collection to JSON

mongoexport --uri="mongodb://localhost:27017" \

  --db=mydatabase \

  --collection=mycollection \

  --out=mycollection.json

🔹 Example: Export to CSV (with selected fields)

mongoexport --uri="mongodb://localhost:27017" \

  --db=mydatabase \

  --collection=mycollection \

  --type=csv \

  --fields=name,age,email \

  --out=mycollection.csv

✅ Option 2: Use mongodump

Creates a binary backup of the database or collection (good for restoring later using mongorestore).

🔹 Example: Dump entire database

mongodump --uri="mongodb://localhost:27017" \

  --db=mydatabase \

  --out=./dump/

🔹 Example: Dump a single collection

mongodump --uri="mongodb://localhost:27017" \

  --db=mydatabase \

  --collection=mycollection \

  --out=./dump/

🔄 To Restore Data Later:

🔹 Using mongorestore for a full dump

mongorestore --uri="mongodb://localhost:27017" ./dump/

📦 Install MongoDB Tools

If you don't have the tools installed:

On Ubuntu: sudo apt-get install mongodb-database-tools

On macOS:

brew tap mongodb/brew

brew install mongodb-database-tools

Or download directly:

https://www.mongodb.com/try/download/database-tools 

No comments:

Post a Comment

Popular Posts