DEV Community

Prasanna Kumar T
Prasanna Kumar T

Posted on

MongoDB Hands-on: Working with a Zomato Restaurants Dataset

This professional guide demonstrates a compact, repeatable workflow for common MongoDB tasks using a Zomato-style restaurants dataset. It covers:

  • inserting sample documents,
  • computing top restaurants by rating,
  • counting reviews that mention the word “good”,
  • fetching all reviews for a restaurant,
  • updating and deleting records, and
  • exporting results as JSON/CSV.

All commands are ready to paste into mongosh (except mongoexport, which runs in your system terminal). The examples assume a database named zomato and a collection named restaurants.

Dataset shape (example document)

Typical document structure used in the examples:


Note: the rate field in this dataset is a string of the form "4.1/5". Queries below show how to parse the numeric portion.

1. Insert at least 10 sample records

Use insertMany to add sample documents if you don’t already have data.

2. Top 5 restaurants by rating

Because rate is stored as "4.1/5", use $split + $toDouble to convert it to a numeric value, then aggregate and sort. The pipeline below computes an average per restaurant (useful if a restaurant has multiple documents).


What this returns: top 5 restaurant names with their average numeric rating and total votes.

3. Count reviews that contain the word “good” (case-insensitive)

If reviews_list is an array of subdocuments with a review text field:


Notes:

This counts documents (restaurants) where at least one review contains “good”.

If you need the exact count of matching reviews across all documents, use $unwind and $match then $count:

4. Retrieve all reviews for a specific restaurant (by name)

Replace "Jalsa" with the restaurant of interest.

5. Update a review and delete a record
Update one matching review (in-place)

Example: update a review in San Churro Cafe whose text mentions “Ambience”.

6. Delete a record (example: remove "Spice Elephant")

7. Export results as JSON/CSV

Important: mongoexport is a command-line tool. Run these commands in your system terminal (PowerShell, Bash, or Command Prompt), not inside mongosh.

Export entire collection as JSON

mongoexport --db=zomato --collection=restaurants --out=restaurants.json

Export selected fields as CSV

mongoexport --db=zomato --collection=restaurants \
--type=csv --fields=name,location,rate,votes,cuisines \
--out=restaurants.csv

Export the Top 5 aggregation result (recommended approach: write the aggregation to a temporary collection, then export):

Write the aggregation to a collection named top5_restaurants:

Export that collection:

mongoexport --db=zomato --collection=top5_restaurants --out=top5_restaurants.json
mongoexport --db=zomato --collection=top5_restaurants --type=csv --fields=_id,location,avgRating,totalVotes --out=top5_restaurants.csv

Authentication / remote server: if your MongoDB uses authentication or is remote, supply --uri with the full connection string:

mongoexport --uri="mongodb://user:pass@host:27017/zomato" --collection=restaurants --out=restaurants.json

Practical notes & best practices

  • Run mongoexport from the OS terminal — not from mongosh. Running it inside the shell causes a syntax error.
  • Normalize numeric fields: if you will frequently query by rating, consider storing rate_num as a numeric field (e.g., 4.1) during ingestion to simplify queries and improve performance.
  • Text search: for more advanced review searches (stemming, ranking), consider enabling text indexes and using $text.

Backups: keep JSON exports of critical collections for reproducibility and backup.

Closing

This guide supplies a compact, production-minded set of queries and commands you can use to demonstrate MongoDB skills on the Zomato dataset. Use the commands as-is in mongosh and run the mongoexport commands from your terminal to produce JSON/CSV deliverables.

Top comments (0)