Connection Strings

Connection Strings and Connection Options

Every MongoDB 1,815 client takes a URI, and mongosh 403 , Compass 1,815 , the driver, Mongoose 243,355 and the database tools parse it with the same rules.

The parts of a MongoDB connection URI
The parts of a MongoDB connection URI

The scheme decides how hosts are found. mongodb:// takes a literal comma-separated list of hosts and ports. mongodb+srv:// takes one hostname, asks DNS for its SRV records to build the seed list and a TXT record for default options, and turns TLS on implicitly. SRV is what Atlas 1,815 hands you: a cluster can replace nodes without any client changing configuration, at the cost of a DNS round trip and a hard failure where SRV is blocked.

The path segment is the default database — what db means in mongosh — not an access restriction. Credentials authenticate against authSource, which defaults to admin for SCRAM users, so a URI ending /bookshelf with a user created in admin still needs ?authSource=admin. Percent-encode reserved characters: p@ss:word becomes p%40ss%3Aword. Setting appName is the cheapest win below: every slow query in the log and every db.currentOp() entry then names the process behind it.

Connection options worth setting, and their defaults
Option Default Why change it
maxPoolSize 100 lower on serverless or M0
minPoolSize 0 pre-warm to avoid first-request latency
serverSelectionTimeoutMS 30000 fail fast in a health check
w 1 majority for durability (Read Preference)
appName none tags queries in logs and currentOp

URI options and MongoClient constructor options do the same job. Put deployment facts in the URI so operators can change them without a rebuild, and application facts in code.