mongodb export to csv code example

Example 1: mongodb export entire database

// EXPORT
mongodump -d <database_name> -o <directory_backup>
// IMPORT
mongorestore -d <database_name> <directory_backup>

Example 2: import csv into mongodb

mongoimport --type csv -d database -c collection --headerline --drop index2020.csv

Example 3: how to export mongodb database

Mongo Export/dump
mongodump --out </path> --db <dbName> --host <hostname:portNo> --authenticationDatabase admin --ssl --username <Remote Db User> --password<Remote Db Password>
Example:
mongodump --out ~/Desktop/mongoData/ --db newdata --host cluster0-shard-01-02.xyz.mongodb.net:27017  --authenticationDatabase admin --ssl --username abc --password xyz

Mongo Import/restore
mongorestore </path> --db <dbName> --host <hostname:portNo> --authenticationDatabase admin --ssl --username <Remote Db User> --password<Remote Db Password>
Example:
mongorestore ~/Desktop/mongobkup/ --host cluster0-shard-00-02.xyz.mongodb.net:27017  --authenticationDatabase admin --ssl --username abc --password xyz

Tags:

Go Example