MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)

mongodb query output to file mongoexport query filter example

Today in this article, we shall see how to Redirect Output of Mongo query to a file ( CSV or JSON ) using available tools like MongoExport CLI tools.

You may have required MongoDB query which returns the records collection using needed criteria.

If you need to run Mongoquery and want to store the mongodb query output to file like JSON or CSV file, then please follow the below explained steps.

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.

Using the CLI tool mongoexport to redirect query output 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="mongodb uri" --collection="collection name" --type=csv -q 'MongoDb query" --fields=fields1,field2,field3 --out = "file name"

In the above example,

  • –uri – Specify the hostname and port in the

  • –collection – Specifies the collection to export.

  • –db – Specifies the name of the database

  • –fields – Specifies a field or fields to include in the export. To specify multiple fields, use a comma-separated list of fields.

  • –out – Specifies a file name to which to write the data returned from the query.

  • --query or -qprovides a query as a JSON document (enclosed in quotes) to return matching documents in the export.

Specifies query as a JSON document to return matching documents in the export. The query should be enclosed (enclosed in quotes).

For more details on all the commands options please visit

Example Mongo Query

{"DateAdded":{"$gt":ISODate('2022-01-01T00:00:00.000+00:00')}}

Let’s execute the above query as below using the MongoExport utility,

mongoexport.exe --uri= mongodb://xxxx:xxxx/TheCodeBuzz?readPreference=primary --collection=Books  -q "{\"DateAdded\":{\"$gt\":ISODate('2022-01-01T00:00:00.000+00:00')}}" --type=csv --out=C://opt/sample.csv --fields=UserId,DateAdded

Above we are running a date range query where we are trying to get all the records where “DateAdded” is greater than the 2022 year from Mongo.

We are exporting a CSV for 2 fields i.e UserId, DateAdded

Generated CSV file looks as below,

Redirect Output of MongoDB query to a CSV file,mongodb query output to file, mongodb compass export query result to csv. mongodb export query result to csv python, mongoexport query filter example, mongodb export query result to json, robo 3t export query result to csv, mongoexport query is not valid json, export data to csv from mongodb by using java,

MongoCompass UI – get output from query within UI

If you are using MongoCompass UI or Robo mongo client, both have rich UI features which let you redirect the query output to documents like CSV or JSON as required.

Steps to redirect the query as below,

Step1: Open the collection and click on the Export Collection icon

mongodb query output to file, mongodb compass export query result to csv. mongodb export query result to csv python, mongoexport query filter example, mongodb export query result to json, robo 3t export query result to csv, mongoexport query is not valid json, export data to csv from mongodb by using java,

Step 2: Select an option to export using a filter query. you can select only fields of your choice.

mongodb compass export query result to csv

Step 3: Select the fields that need to be downloaded in the target exported file

mongodb export query result to json

Step 4: Please select the options JSON or CSV as required.

You need to provide an output file path. Next click on the EXPORT button.

mongodb output as CSV or JSON

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

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 *