MongoDB Connection URI Format — MongoDB Technical Interview Questions & Answers

mongodb:// and mongodb+srv:// URI options explained.

MongoDB Connection URI Format

  • Simple meaning: URI = protocol + user + password + hosts + database + query options.
  • Local: mongodb://127.0.0.1:27017/college
  • Auth: mongodb://asha:secret@localhost:27017/college?authSource=admin
  • Atlas: mongodb+srv://asha:secret@cluster0.xxxxx.mongodb.net/college?retryWrites=true&w=majority
  • Common mistake: unescaped @ in password. Wrong authSource. srv on a local single host.
  • Interview tip: Draw the pieces. srv vs standard. retryWrites for Atlas.

Technical Interview Tip: Write one local and one Atlas URI with labels.

Code Examples for MongoDB Connection URI Format

MongoDB Connection URI Format — working example

// URI forms used by mongosh and drivers:
// mongodb://user:pass@localhost:27017/prep_pilot?authSource=admin
// mongodb+srv://user:pass@cluster0.example.mongodb.net/prep_pilot?retryWrites=true&w=majority

mongosh "mongodb://localhost:27017/prep_pilot"
db.users.findOne()

MongoDB Connection URI Format — mongodb:// is standard; mongodb+srv:// is Atlas DNS seed-list style.

How to Explain MongoDB Connection URI Format to an Interviewer

Parts of a Mongo URI?

User, host, database, options mongodb:// or mongodb+srv://