⬅ Previous Topic
Creating and Managing Databases in AtlasNext Topic ⮕
Backing Up MongoDB using mongodump⬅ Previous Topic
Creating and Managing Databases in AtlasNext Topic ⮕
Backing Up MongoDB using mongodumpMongoDB Atlas is a cloud-based database service that allows you to host MongoDB clusters with scalability, monitoring, and security built-in. To use it in your local development environment, you need to connect your application using the connection string provided by Atlas.
Once the cluster is created, go to "Database Access" → "Add New Database User".
Read and write to any database
Under "Network Access", click "Add IP Address" → Add your current IP (or use 0.0.0.0
to allow all for testing purposes).
Go to "Database" → "Connect" → "Connect your application".
mongodb+srv://<user>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority
)Replace <user>
and <password>
with your actual credentials.
Install Mongoose:
npm install mongoose
Sample code:
// file: connect.js
const mongoose = require('mongoose');
const uri = "mongodb+srv://yourUsername:yourPassword@cluster0.mongodb.net/test?retryWrites=true&w=majority";
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("MongoDB connected successfully!");
})
.catch((err) => {
console.error("MongoDB connection error:", err);
});
MongoDB connected successfully!
Explanation: You use Mongoose to connect to the Atlas cluster using the provided URI. The useNewUrlParser
and useUnifiedTopology
options are recommended for compatibility and stability.
Install PyMongo:
pip install pymongo
Sample code:
# file: connect.py
from pymongo import MongoClient
uri = "mongodb+srv://yourUsername:yourPassword@cluster0.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri)
# Access a test database
db = client.test
print("Collections in test DB:", db.list_collection_names())
Collections in test DB: []
Explanation: We use MongoClient
from PyMongo to connect. The db.list_collection_names()
lists existing collections in the test
database.
Q: Why do we need to whitelist IP addresses on MongoDB Atlas?
A: For security. MongoDB Atlas restricts access only to known IP addresses to prevent unauthorized connections.
Q: Can I use this URI in production?
A: Yes, but you should store credentials in environment variables or use secrets management tools — never hard-code sensitive credentials in production apps.
Connecting your MongoDB Atlas cluster to local applications is straightforward once you configure users, whitelist IPs, and use the provided URI. You can use Node.js (Mongoose) or Python (PyMongo) depending on your stack.
In the next topic, we’ll learn how to build and run real-world applications using this connection.
⬅ Previous Topic
Creating and Managing Databases in AtlasNext Topic ⮕
Backing Up MongoDB using mongodumpYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.