← Back to MongoDB Course | Chapter 10: MongoDB with Node.js | Lesson 5 of 7

Atlas से Connect करना

Atlas से connect करने का मतलब है आपके cluster से mongodb+srv connection string इस्तेमाल करना, एक environment variable में रखा हुआ।
Syntax
javascript
await mongoose.connect("mongodb+srv://username:[email protected]/database");

Atlas से Connect करना

Atlas Connect dialog से connection string copy करें, database user का password और default database substitute करें, और इसे MONGODB_URI में store करें।

यह सुनिश्चित करें कि आपका IP Network Access में allowed है। serverSelectionTimeoutMS जैसे options cluster unreachable होने पर fast fail होने में मदद करते हैं।

Note: password में special characters URL-encode करें।

उदाहरण: Connecting to Atlas

javascript
// .env
// MONGODB_URI=mongodb+srv://appUser:p%[email protected]/shop?retryWrites=true&w=majority

require("dotenv").config();
const mongoose = require("mongoose");

mongoose.connect(process.env.MONGODB_URI, { serverSelectionTimeoutMS: 5000 })
  .then(() => console.log("Connected to Atlas"))
  .catch((err) => { console.error("Connection failed:", err.message); process.exit(1); });

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. connection string commit करना
  2. अपना IP whitelist करना भूल जाना
  3. special password characters encode न करना
चैप्टर सारांश
  • mongodb+srv string इस्तेमाल करें
  • इसे एक environment variable में रखें
  • Atlas में अपना IP allow करें
  • Fast fail के लिए timeouts set करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.