Hello everyone,
I’ve recently started my journey into the world of databases and chose MongoDB as my starting point. As one of the most popular NoSQL databases, MongoDB offers flexibility by storing data in JSON-like documents instead of rigid relational tables. This makes it particularly effective for handling unstructured or evolving datasets while ensuring scalability and adaptability—qualities that are vital for modern applications.
1.Getting Started
I began by installing MongoDB and setting up a database named Business with a collection called Review. This served as the foundation for my experiments with data storage and manipulation.
2.Importing the Dataset
To work with real-world data, I imported a dataset named yelp.json containing about 1000 records. This dataset provided the context for applying queries and understanding how MongoDB processes data.
3.Inserting Multiple Documents with insertMany()
After loading the dataset, I practiced adding additional records by inserting 10 documents into the Review collection using the insertMany() method.
After inserting the datasets
4. Query to Find Top 5 Businesses with Highest Average Rating
I utilized the aggregate function to identify the top five businesses based on their average ratings.
db.reviews.aggregate([
{ $group: { _id: "$business_id", avg_rating: { $avg: "$stars" } } },
{ $sort: { avg_rating: -1 } },
{ $limit: 5 }
])
5. Query to Count Reviews Containing the Word “good”
To measure customer sentiment, I applied the countDocuments method to find the number of reviews containing the keyword “good.”
db.reviews.countDocuments({ text: /good/i })
6.Query to Retrieve Reviews for a Specific Business ID
I retrieved all reviews linked to a particular business ID using the find() method.
db.reviews.find({ business_id: "B101" })
7.Update a review and delete a record
7.1: Updating a Review
I updated a single document in the collection by using the updateOne() method.
db.reviews.updateOne(
{ review_id: "R107" },
{
$set: {
stars: 4,
text: "The food was good and the service was friendly. I enjoyed the atmosphere, but I felt it was a bit crowded."
}
}
);
7.2: Deleting a Record
I removed one document from the collection by applying the deleteOne() method.
db.reviews.deleteOne(
{ review_id: "R104" }
);
Conclusion
This mini-project provided me with valuable hands-on experience in MongoDB. I explored essential operations such as inserting documents, retrieving data, applying filters, performing aggregations, and executing update/delete actions.
Through this, I gained an appreciation of how MongoDB handles dynamic, unstructured data compared to traditional relational databases. Moving forward, I am excited to explore advanced MongoDB features like replication, sharding, and performance optimization, which are crucial for developing highly scalable, real-world systems.
Top comments (0)