MongoDB Atlas offers a powerful, cloud-based platform for managing NoSQL databases. This guide provides a practical walkthrough of essential database operations, including Create, Read (Query), Update, and Delete (CRUD) functions, along with methods for exporting your data. Perfect for data engineers and database enthusiasts, this tutorial will equip you with the skills to effectively interact with your MongoDB Atlas clusters.

πŸ—‚οΈ Setting up MongoDB Atlas

Embarking on your MongoDB journey begins with setting up a cluster on MongoDB Atlas. Follow these steps to get started:

  1. Navigate to the official MongoDB Atlas website.
  2. Opt for a free cluster, selecting the Shared Tier option for cost-effective deployment.
  3. Configure your Network Access to allow connections:
    • Go to Network Access β†’ Add IP Address β†’ Allow access from anywhere (0.0.0.0/0).
  4. Establish a dedicated database user. Remember your chosen username and password, as these will be crucial for authentication. For instance:
    Username: 22cs098
    Password: NAVEEN
    
  5. Once your cluster is operational, click on β€œConnect β†’ Connect using MongoDB Shell” to retrieve your unique connection string. This string is vital for connecting to your database.

πŸ’» Connect from Mongo Shell

With your cluster set up, the next step is to connect using the Mongo Shell.
Open your terminal (PowerShell or Command Prompt) and execute the following command, replacing the placeholder with your actual connection string and username:

mongosh "mongodb+srv://m0.wpjmxqh.mongodb.net/" --apiVersion 1 --username 22cs098_db_user

You will then be prompted to enter your password:

Enter password: NAVEEN

A successful connection will display a prompt similar to this, indicating you’re ready to interact with your database:

Atlas atlas-xxxx-shard-0 [primary]>

πŸ“₯ Create a Database and Insert Records

Let’s populate our database with some initial data. We’ll start by switching to (or automatically creating) a database named businessDB and then inserting several business review records.
First, select your database:

use businessDB

Now, insert a collection of 10 sample business reviews using insertMany():

db.reviews.insertMany([
  { "business_id": "B001", "name": "Cafe Aroma", "rating": 4.6, "review": "Good food and fast service!", "date": "2025-11-07" },
  { "business_id": "B002", "name": "Pizza Palace", "rating": 4.8, "review": "Amazing crust and cheese quality!", "date": "2025-11-07" },
  { "business_id": "B003", "name": "Tea Time", "rating": 4.2, "review": "Nice ambience and friendly staff.", "date": "2025-11-07" },
  { "business_id": "B004", "name": "Sweet Treats", "rating": 3.9, "review": "Desserts were good but service was slow.", "date": "2025-11-07" },
  { "business_id": "B005", "name": "Veggie Delight", "rating": 4.1, "review": "Healthy food with good taste.", "date": "2025-11-07" },
  { "business_id": "B006", "name": "Burger Hub", "rating": 4.9, "review": "Best burgers ever!", "date": "2025-11-07" },
  { "business_id": "B007", "name": "Ocean Dine", "rating": 4.7, "review": "Fresh seafood and great view.", "date": "2025-11-07" },
  { "business_id": "B008", "name": "Spice Route", "rating": 3.8, "review": "Food was okay, but spicy.", "date": "2025-11-07" },
  { "business_id": "B009", "name": "Bakers Street", "rating": 4.5, "review": "Good pastries and coffee.", "date": "2025-11-07" },
  { "business_id": "B010", "name": "Quick Bite", "rating": 4.0, "review": "Good service and clean place.", "date": "2025-11-07" }
])

πŸ” Querying Data

Retrieving specific information from your database is a fundamental operation. Here’s how to execute various queries:

πŸ† Top 5 Businesses by Rating

To find the businesses with the highest ratings, sort the reviews collection in descending order by rating and limit the results to five:

db.reviews.find().sort({ rating: -1 }).limit(5)

πŸ”€ Count of Reviews Containing β€œgood”

Discover how many reviews mention the word “good” (case-insensitive) using countDocuments() with a regular expression:

db.reviews.countDocuments({ review: /good/i })

πŸͺ Get Reviews for a Specific Business ID

To fetch all reviews associated with a particular business, use a direct query on the business_id field:

db.reviews.find({ business_id: "B005" })

✏️ Update and Delete Operations

Maintaining data integrity involves modifying existing records and removing outdated ones.

✏️ Update a Review

To update a specific review, for instance, changing the rating and text for B005, use updateOne():

db.reviews.updateOne(
  { business_id: "B005" },
  { $set: { rating: 4.3, review: "Updated: Great taste and fresh ingredients!" } }
)

πŸ—‘οΈ Delete a Record

If a business record is no longer needed, you can remove it using deleteOne(). Here, we delete the record for B010:

db.reviews.deleteOne({ business_id: "B010" })

πŸ“€ Export Data to JSON/CSV

After performing CRUD operations, you might need to export your data for analysis or migration. First, exit the Mongo shell:

exit

Then, from your operating system’s terminal (e.g., PowerShell or Command Prompt), use mongoexport.

πŸ“„ Export as CSV

To export your reviews collection into a CSV file, specifying the desired fields:

mongoexport --uri="mongodb+srv://22cs098_db_user:[email protected]/businessDB" --collection=reviews --type=csv --fields=business_id,name,rating,review,date --out=reviews.csv

πŸ“¦ Export as JSON

For a JSON export of your reviews collection:

mongoexport --uri="mongodb+srv://22cs098_db_user:[email protected]/businessDB" --collection=reviews --out=reviews.json

πŸ“Š View the Exported Files

You can now examine your exported data:

  • Open `reviews.csv` with applications like Excel or VS Code.
  • View `reviews.json` in any text editor to see the raw JSON structure.

βœ… Summary

This tutorial has walked you through the fundamental operations within MongoDB Atlas. You’ve learned how to:

Operation Command Type Example
Insert insertMany() Adding multiple reviews
Query find(), countDocuments() Searching and filtering data
Update updateOne() Modifying existing records
Delete deleteOne() Removing specific entries
Export mongoexport Generating CSV and JSON outputs

🎯 Final Thoughts

MongoDB Atlas streamlines cloud database management, simplifies CRUD operations, and offers flexible data export options. These skills are invaluable for anyone working in data engineering and database administration.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed