Mongo – Export collection as CSV or JSON

Mongo Export collection as CSV or JSON

Today in this article, we shall see how to perform MongoDB – Export collection as CSV or JSON using available tools like UICompass and CLI tools.

Today in this article, we will cover below aspects,

We shall see how to export the MongoDB collection as CSV or JSON using Compass UI and mongoexport utility tools available.

MongoCompass UI- export documents as CSV or JSON

If you are using MongoCompass UI or Robo mongo client, both have rich UI features which let you export documents as CSV or JSON as required.

Step to export the document or collection as CSV

Open the collection and click on the Export Collection icon

Select an option to export the full collection or export using a filter query. you can select only fields of your choice.

Please select the options as JSON or CSV as required. You need to provide an output file path. Next click on the EXPORT button.

blank

After successful export, you shall see a generated CSV file with the required details.

blank

Using the CLI tool mongoexport to export the data as CSV or JSON

Using the CLI tool mongoexport to export the data as CSV or JSON

mongoexport utility can be found at the below location,

C:\Program Files\MongoDB\Server.0\bin

Command

mongoexport.exe --uri="your mongodb uri" --collection="your collection name" --type=csv --fields=fields1,field2,field3 --out = "Output file name"

Example

mongoexport.exe --uri=mongodb://xxxxxx:xxxxx/Book?readPreference=primary --collection=Book --type=csv --fields=Name,Price,Author,BooksStore --out=C://opt/test5.csv

blank

Generated CSV file looks as below,

blank

Reference :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

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