You can use connection strings to define connections between MongoDB instances and the following destinations:
Your applications when you connect using drivers.
Tools such as MongoDB Compass and MongoDB Shell (mongosh).
To connect to your cluster, you can use one of these connection string formats:
SRV connection strings use the
mongodb+srv://prefix to simplify connecting to a cluster. SRV connection strings automatically include all seed list hosts, which supports server rotation without client reconfiguration. When possible, use SRV connection strings over standard connection strings.Standard connection strings use the
mongodb://prefix and require you to include all cluster members in replica sets and sharded clusters.
Use the selectors at the top of the page to choose your deployment type and connection string format. Complete the following steps to find your connection string.
Find Your MongoDB Atlas Connection String
To find your MongoDB Atlas connection string using the Atlas CLI, install and connect from the Atlas CLI, then run the following command. Replace <clusterName> with the name of the MongoDB Atlas cluster and replace <projectId> with the project ID.
atlas clusters connectionStrings describe <clusterName> --projectId <projectId>
Your MongoDB Atlas connection string resembles the following example:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
To learn more, see atlas clusters connectionStrings describe.
Atlas Cluster that Authenticates with AWS IAM Credentials
The following example connects to an Atlas cluster that has been configured to support authentication via AWS IAM credentials:
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
Connecting to Atlas using AWS IAM credentials in this manner uses the MONGODB-AWS authentication mechanism and the $external authSource.
If you use an AWS session token, specify an AWS_SESSION_TOKEN in the authMechanismProperties value, as shown in the following example:
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
Note
If the AWS access key ID, the secret access key, or the session token include the following characters:
$ : / ? # [ ] @
those characters must be converted using percent encoding.
You can also set these credentials on your platform using standard AWS IAM environment variables. mongosh checks for the following environment variables when you use the MONGODB-AWS authentication mechanism:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN
If set, these credentials do not need to be specified in the connection string.
The following example sets these environment variables in the bash shell:
export AWS_ACCESS_KEY_ID='<aws access key id>' export AWS_SECRET_ACCESS_KEY='<aws secret access key>' export AWS_SESSION_TOKEN='<aws session token>'
Syntax for setting environment variables in other shells will be different. Consult the documentation for your platform for more information.
To verify that these environment variables have been set, run the following command:
env | grep AWS
Once set, the following example connects to an Atlas cluster using these environment variables:
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
Find Your MongoDB Atlas Connection String
To find your MongoDB Atlas connection string in the Atlas UI, follow these steps:
In the MongoDB Atlas UI, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Clusters under the Database heading.
The Clusters page displays.
Open the Connection Method dialog.
Click Connect for the cluster to which you want to connect.
Click Choose a Connection Method. MongoDB Atlas selects Standard Connection by default. To connect using a private endpoint, select Private Endpoint.
Follow instructions for the connection method you selected.
If you selected Drivers, select your driver and version. If you selected a tool, download the tool.
Select Connect To Cluster.
Copy the connection string. Replace
<password>and<username>in the connection string with the database user's credentials.
Your MongoDB Atlas connection string resembles the following example:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority
Atlas Cluster that Authenticates with AWS IAM Credentials
The following example connects to an Atlas cluster that has been configured to support authentication via AWS IAM credentials:
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
Connecting to Atlas using AWS IAM credentials in this manner uses the MONGODB-AWS authentication mechanism and the $external authSource.
If you use an AWS session token, specify an AWS_SESSION_TOKEN in the authMechanismProperties value, as shown in the following example:
mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>
Note
If the AWS access key ID, the secret access key, or the session token include the following characters:
$ : / ? # [ ] @
those characters must be converted using percent encoding.
You can also set these credentials on your platform using standard AWS IAM environment variables. mongosh checks for the following environment variables when you use the MONGODB-AWS authentication mechanism:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN
If set, these credentials do not need to be specified in the connection string.
The following example sets these environment variables in the bash shell:
export AWS_ACCESS_KEY_ID='<aws access key id>' export AWS_SECRET_ACCESS_KEY='<aws secret access key>' export AWS_SESSION_TOKEN='<aws session token>'
Syntax for setting environment variables in other shells will be different. Consult the documentation for your platform for more information.
To verify that these environment variables have been set, run the following command:
env | grep AWS
Once set, the following example connects to an Atlas cluster using these environment variables:
mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following replica set connection string includes these elements:
The hostnames of the
mongodinstance(s) as listed in the replica set configuration.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following replica set connection string includes these elements:
The hostnames of the
mongodinstance(s) as listed in the replica set configuration.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following sharded cluster connection string includes these elements:
The hostnames of the
mongosinstances of the sharded cluster.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following sharded cluster connection string includes these elements:
The hostnames of the
mongosinstances of the sharded cluster.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following connection string for a standalone node includes these elements:
The hostname of the
mongodinstance.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Find Your Self-Hosted Deployment's Connection String
If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.
If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.
The following connection string for a standalone node includes these elements:
The hostname of the
mongodinstance.Authentication as user
myDatabaseUserwith the passwordD1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
If the username or password includes the following characters, those characters must be converted using percent encoding:
$ : / ? # [ ] @
Additional Connection String Examples
The following examples show connection strings for common connection targets.
admin Database Running Locally
The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost
records Database Running Locally
The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records
UNIX Domain Socket
When you connect to a UNIX domain socket, use a URL-encoded connection string.
The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Hosts
The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:
Note
For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhost
The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl
Replica Set with Read Distribution
The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary
Replica Set with a High Level of Write Concern
The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000
Sharded Cluster
The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
Learn More
For a full list of connection string options, see Connection String Options.