This content originally appeared on DEV Community and was authored by MUKHIL S
In this blog post, we'll explore how to perform basic CRUD operations and queries on a MongoDB database named mydb
with a collection called yelp
, based on a Yelp dataset. We'll use MongoDB's JavaScript-based query syntax to insert records, find top-rated businesses, count reviews containing specific words, retrieve reviews for a business, update a review, and delete a record. Let's dive in!
1. Inserting Records into the Yelp Collection
To populate the yelp
collection, we can use the insertMany
method to add multiple documents at once. Below is an example of inserting 10 review documents, each with fields like business_id
, date
, review_id
, stars
, text
, type
, user_id
, cool
, useful
, and funny
.
db.yelp.insertMany([
{
business_id: "A1B2C3D4E5F6G7H8I9J0K1",
date: new Date("2023-01-01"),
review_id: "R1S2T3U4V5W6X7Y8Z9A0B1",
stars: 4,
text: "Great food and ambiance, will come back!",
type: "review",
user_id: "U1V2W3X4Y5Z6A7B8C9D0E1",
cool: 1,
useful: 2,
funny: 0
},
{
business_id: "A1B2C3D4E5F6G7H8I9J0K1",
date: new Date("2023-02-15"),
review_id: "R2T3U4V5W6X7Y8Z9A0B1C2",
stars: 5,
text: "Amazing service, loved the pizza!",
type: "review",
user_id: "U2W3X4Y5Z6A7B8C9D0E1F2",
cool: 2,
useful: 3,
funny: 1
},
{
business_id: "B2C3D4E5F6G7H8I9J0K1L2",
date: new Date("2023-03-10"),
review_id: "R3U4V5W6X7Y8Z9A0B1C2D3",
stars: 3,
text: "Decent food, but slow service.",
type: "review",
user_id: "U3X4Y5Z6A7B8C9D0E1F2G3",
cool: 0,
useful: 1,
funny: 0
},
{
business_id: "B2C3D4E5F6G7H8I9J0K1L2",
date: new Date("2023-04-20"),
review_id: "R4V5W6X7Y8Z9A0B1C2D3E4",
stars: 4,
text: "Good burgers, nice patio.",
type: "review",
user_id: "U4Y5Z6A7B8C9D0E1F2G3H4",
cool: 1,
useful: 1,
funny: 0
},
{
business_id: "C3D4E5F6G7H8I9J0K1L2M3",
date: new Date("2023-05-05"),
review_id: "R5W6X7Y8Z9A0B1C2D3E4F5",
stars: 5,
text: "Best sushi ever, fresh and tasty!",
type: "review",
user_id: "U5Z6A7B8C9D0E1F2G3H4I5",
cool: 3,
useful: 4,
funny: 2
},
{
business_id: "C3D4E5F6G7H8I9J0K1L2M3",
date: new Date("2023-06-12"),
review_id: "R6X7Y8Z9A0B1C2D3E4F5G6",
stars: 4,
text: "Really good desserts, friendly staff.",
type: "review",
user_id: "U6A7B8C9D0E1F2G3H4I5J6",
cool: 2,
useful: 2,
funny: 1
},
{
business_id: "D4E5F6G7H8I9J0K1L2M3N4",
date: new Date("2023-07-19"),
review_id: "R7Y8Z9A0B1C2D3E4F5G6H7",
stars: 5,
text: "Fantastic experience, highly recommend!",
type: "review",
user_id: "U7B8C9D0E1F2G3H4I5J6K7",
cool: 4,
useful: 5,
funny: 3
},
{
business_id: "D4E5F6G7H8I9J0K1L2M3N4",
date: new Date("2023-08-25"),
review_id: "R8Z9A0B1C2D3E4F5G6H7I8",
stars: 3,
text: "Average food, great drinks.",
type: "review",
user_id: "U8C9D0E1F2G3H4I5J6K7L8",
cool: 0,
useful: 1,
funny: 0
},
{
business_id: "E5F6G7H8I9J0K1L2M3N4O5",
date: new Date("2023-09-30"),
review_id: "R9A0B1C2D3E4F5G6H7I8J9",
stars: 4,
text: "Loved the vibe, food was good.",
type: "review",
user_id: "U9D0E1F2G3H4I5J6K7L8M9",
cool: 2,
useful: 3,
funny: 1
},
{
business_id: "E5F6G7H8I9J0K1L2M3N4O5",
date: new Date("2023-10-15"),
review_id: "S0B1C2D3E4F5G6H7I8J9K0",
stars: 5,
text: "Incredible food, great service!",
type: "review",
user_id: "V0E1F2G3H4I5J6K7L8M9N0",
cool: 3,
useful: 4,
funny: 2
}
]);
Each document represents a review with fields matching the structure of a Yelp dataset (e.g., business_id
, stars
, text
). Note that the 'date' field uses MongoDB's new Date()
to store dates properly.
2. Finding the Top 5 Businesses by Average Rating
To find the top 5 businesses with the highest average rating, we use MongoDB's aggregation pipeline. The pipeline groups reviews by 'business_id', computes the average 'stars', sorts in descending order, and limits to 5 results.
db.yelp.aggregate([
{ $group: { _id: "$business_id", avg_rating: { $avg: "$stars" } } },
{ $sort: { avg_rating: -1 } },
{ $limit: 5 }
]);
For the inserted data, this query returns:
- 'C3D4E5F6G7H8I9J0K1L2M3': 4.5
- 'E5F6G7H8I9J0K1L2M3N4O5': 4.5
- 'A1B2C3D4E5F6G7H8I9J0K1': 4.5
- 'D4E5F6G7H8I9J0K1L2M3N4': 4
- 'B2C3D4E5F6G7H8I9J0K1L2': 3.5
3. Counting Reviews Containing the Word “Good”
To count reviews with the word 'good' in the 'text' field, we use countDocuments
with a case-insensitive regex.
db.yelp.countDocuments({ text: { $regex: "good", $options: "i" } });
This returns '3' for the inserted data, as three reviews contain 'good' (review IDs 'R4V5W6X7Y8Z9A0B1C2D3E4', 'R6X7Y8Z9A0B1C2D3E4F5G6', 'R9A0B1C2D3E4F5G6H7I8J9').
4. Retrieving All Reviews for a Specific Business
To fetch all reviews for a specific 'business_id', we use the find
method. For example, to get reviews for '9yKzy9PApeiPPOUJEtnvkg' (from the sample Yelp data):
db.yelp.find({ business_id: "9yKzy9PApeiPPOUJEtnvkg" }).toArray();
This returns all matching reviews. If the sample review from the Yelp CSV is inserted, it would return that review.
5. Updating a Review
To update a review, we use updateOne
to modify specific fields. For example, to update the review with 'review_id' 'fWKvX83p0-ka4JS3dc6E5A' (from the sample data, if inserted):
db.yelp.updateOne(
{ review_id: "fWKvX83p0-ka4JS3dc6E5A" },
{ $set: { stars: 3, text: "Updated review: Food was okay, service was slow." } }
);
This sets 'stars' to 3 and updates the 'text' field for the specified review.
6. Deleting a Record
To delete a review, we use deleteOne
. For example, to remove the review with 'review_id' 'R1S2T3U4V5W6X7Y8Z9A0B1':
db.yelp.deleteOne({ review_id: "R1S2T3U4V5W6X7Y8Z9A0B1" });
This removes the specified review from the collection.
Notes
- These queries assume you're using MongoDB with the 'yelp' collection in the 'mydb' database.
- The 'date' field uses MongoDB's
Date
object for proper storage. When importing a CSV, ensure dates are parsed correctly (e.g.,new Date("YYYY-MM-DD")
). - The regex in the 'good' query is case-insensitive ('$options: "i"').
- To import a large 'yelp.csv' file, you can use MongoDB's
mongoimport
tool or a script to parse and insert the data. - If you encounter issues or need help with specific MongoDB versions, importing data, or running these queries, feel free to share more details!
This setup provides a solid foundation for working with Yelp data in MongoDB. You can extend these queries for more complex analyses, like filtering by date or combining conditions. Happy coding!
these are my sample images and work are mentioned in the image
This content originally appeared on DEV Community and was authored by MUKHIL S

MUKHIL S | Sciencx (2025-08-21T09:12:04+00:00) Working with Yelp Data in MongoDB: CRUD Operations and Queries. Retrieved from https://www.scien.cx/2025/08/21/working-with-yelp-data-in-mongodb-crud-operations-and-queries/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.