How to export data in .csv file in MongoDB?

Connect with

export data in csv in mongodb | mongodb export query result to csvHow to export data in csv in MongoDB using mongoexport command. Learn utility for MongoDB export query result to csv using mongoexport utility. Its very easy and step by step process.

1. export data in csv in MongoDB

Well, MongoDB export query result to csv file using mongoexport utility. when you encounter to export your data to a file like .csv. You might think how to export data into .csv file or excel file in MongoDB? but believe me, this is very simple. Exporting of data is one-step process in mongoDB using mongoexport utility command. I will explain how to pass parmeter and its value for export data into csv file and late you can convert into excell sheet too.

On the command line type or copy the following line and change respective parameter values according to your need.

syntax of mongoexport utility:

mongoexport -d dbName -c collectionName --type=csv --fields fieldsNameWithCommaSeparated -q 'mongoDBquery' --out /pathOfYourChoice/csvFileName.csv

Example as follow:

mongoexport -d myServiceDB -c myDeviceDetails --type=csv --fields mobileno,name -q '{"city":"/Delhi/"}' --out /tmp/myFileName.csv

2. Arguments of mongoexport tool for exporting result into csv

Here, I’m trying to explain each arguments, which I have provided above to export the query result into csv file.

  • mongoexport
  • : this is a utility tool in mongoDB under the ../bin/ directory , which is being used to export the data in either .csv or .json format and same exported data can be imported by using mongoimport utility tool.

  • -d myServiceDB
  • : here -d followed by name of the database , where your collection reside.

  • -c myDeviceDetails
  • -c followed by name of your collection from which you want to export your data in either .csv or .json format.

  • --type=csv
  • –type=csv means , exported data format in .csv not in .json. default is .json format.

  • --fields mobileno,name
  • –fields followed by name of the fields which you want to include in .csv file, here fields are comma (,) separated.

  • -q '{"city":"/Delhi/"}'
  • : provide the mongoDB query for which data need to export. Query is optional here , if you not provide query, export all data into csv file for provided collection.

  • --out /tmp/myFileName.csv
  • : provide the name of file with absolute location, where you want to generate exported data in .csv format.

Reference

You can visit docs.mongodb.com for more details.

Thanks for visiting this post for export data in csv in MongoDB. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented database.
Happy Learning 🙂 for MongoDB export query result to csv using mongoexport utility.
Your comments are welcome to improve this post.


Connect with

Leave a Comment

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