How to export MongoDB collection to CSV

mongoexport command is used to export MongoDB collection into CSV or JSON format.

Syntax

mongoexport --db DB_NAME --collection COLLECTION_NAME --fields FIELDS_HERE --type=csv --out NAME_OF_OUT_FILE

Where

DB_NAME = name of the database
COLLECTION_NAME = name of the collection
FIELDS_HERE = comma separated list of fields.
NAME_OF_OUT_FILE = name of the CSV file

By default MongoDB connect to localhost port 27017

Example

Export mongodb collection to CSV file

mongoexport --db main --collection users --fields  name,email,level --type csv --out users.csv

In this case, we export fields “name,email,level” from “users” collection in database “main”. Result will be saved to file users.csv in current directory.

Back to MongoDB

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *