MongoDB Export Top N records

Mongo Export collection as CSV or JSON

Today in this article, we shall see how to perform MongoDB export top n records as CSV or JSON using available tools like Mongo Atlas UI 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 Atlas UI- export documents

If you are using MongoCompass Atlas 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.

The above query will export the entire collection into JSON or CSV.

Select the Top 100 records from Mongo Collection

Let’s select the Top N records from Mongo Collection.

Select the Top 100 records from Mongo Collection

Above we are exporting the top 100 records without any Filter or Sort logic.

Here, we will be able to export the top 100 records in JSON or CSV format easily

blank

Please select the options 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 the top 100 records exported CSV file with the required details.

export the Top 100 records from Mongo Collection

MongoShell Query

Below sample query gives the output of the top 100 records without any filters.

db.Books.Find({}).limit(100)

The above query can be extended with additional filters as required.

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 FilesMongoDBServer4.0bin

Command

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

Example

blank

Generated CSV file looks as below,

blank

Reference :



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 *