Learn how to connect to MongoDB clusters whether they are hosted on MongoDB Atlas or self-managed.
Connection String Formats
To connect to your deployment, you need a connection string. The connection string format depends on whether you connect to a replica set, sharded cluster, or standalone deployment.
Replica Set:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
Sharded Cluster:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin
Standalone:
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:
$ : / ? # [ ] @
For detailed information about connection string formats and options, see Connection Strings.
Get Your Connection String
How you obtain your connection string depends on whether your deployment is hosted on MongoDB Atlas or self-managed.
MongoDB Atlas Deployments
To connect to a cluster hosted on MongoDB Atlas, see Connect to an Atlas Cluster, which covers how to:
Get your connection string via the Atlas CLI or Atlas UI.
Configure your IP access list.
Create database users.
Use various connection methods including drivers, Compass,
mongosh, and more.
Self-Managed Deployments
Before you connect to a self-managed deployment:
Ensure your MongoDB deployment is running and accessible.
Create a database user with the appropriate privileges.
Verify network connectivity between your client and the MongoDB deployment.
To construct your connection string, use the appropriate connection string format for your deployment topology.
Connection Methods
After you have your connection string, you can connect using the following methods:
Connect to a Cluster via Client Libraries - MongoDB drivers for various programming languages
Connect to a Cluster via Compass - MongoDB Compass GUI
Connect to a Cluster via mongosh -
mongoshConnect to a Cluster via VS Code - MongoDB for VS Code
Connect to a Cluster via the BI Connector - MongoDB BI Connector
Command Line Tools - mongodump, mongorestore, and other tools