Get Started Free

More information about MongoDB connection strings

Almost every MongoDB driver and tool, from mongosh to Compass to your application code, connects using a connection string URI. This guide explains how a MongoDB URI is structured, what each part does, and which options you'll reach for most often.

What is a MongoDB connection string?

A connection string is a single URL that tells a MongoDB driver where your database lives and how to connect to it, the servers, the credentials, and any connection options. Instead of configuring host, port and auth separately, you pass one string and the driver parses the rest.

The two URI schemes

Components of a MongoDB URI

General format

mongodb://[username:password@]host1[:port1][,host2[:port2]...]/[database][?options]

Example

mongodb://appUser:s3cret@db1.example.com:27017,db2.example.com:27017/shop?replicaSet=rs0&authSource=admin&tls=true

This connects to a two-member replica set rs0, authenticates user appUser against the admin database, selects the shop database, and requires TLS.

Common options

Authentication

Topology

Security & reliability

Performance

Tips for building a MongoDB URI

Frequently asked questions

What is the default MongoDB port?

27017. If your server listens on a different port, include it explicitly as host:port. (DNS seedlist URIs don't use an explicit port.)

What's the difference between mongodb:// and mongodb+srv://?

mongodb:// lists servers explicitly; mongodb+srv:// takes a single hostname and resolves the member list and default options from DNS. Atlas provides the +srv form, and it enables TLS by default.

My password has special characters, will it work?

Yes. Characters like @, :, /, ? and # must be percent-encoded in the URI. This builder encodes the username and password for you.

Do I need authSource?

Only when your user is defined in a database other than the one you're connecting to (commonly admin). The builder adds authSource only when it differs from MongoDB's default.

VisuaLeaf

Connect and explore MongoDB visually with VisuaLeaf

Once your connection string is ready, VisuaLeaf turns it into a full workspace, browse collections, build queries and aggregations visually, design schemas, and chart your data. One modern client for MongoDB and SQL.

MongoDB & SQL in one client
Visual query & aggregation builders
Live visual schema diagrams
Charts, dashboards & query profiler
Connection manager for local, cloud & teams
Free Community Edition