MongoDB- How to Redirect Output of Mongo query to a file ( CSV or JSON)
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,
- Using the CLI tool mongoexport to redirect query output CSV or JSON
- MongoCompass UI – get output from query within UI
- Step1: Open the collection and click on the Export Collection icon
- Step 2: Select an option to export using a filter query. you can select only fields of your choice.
- Step 3: Select the fields that need to be downloaded in the target exported file
- Step 4: Please select the options JSON or CSV as required.
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 -q
– provides 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,
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
Step 2: Select an option to export using a filter query. you can select only fields of your choice.
Step 3: Select the fields that need to be downloaded in the target exported file
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.
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.