Connecting to MongoDB
Connect Agent M to your MongoDB database, whether it's a local instance, MongoDB Atlas, or a self-hosted server.
Prerequisites
- A MongoDB instance (local, Atlas, or self-hosted)
- Connection credentials (if authentication is enabled)
Connection String Format
MongoDB uses connection strings to specify how to connect to your database:
Standard Connection
mongodb://[username]:[password]@[host]:[port]/[database]MongoDB Atlas (SRV)
mongodb+srv://[username]:[password]@[cluster].mongodb.net/[database]Connection Methods
1Local MongoDB
For a local MongoDB instance running on your machine:
mongodb://localhost:27017If authentication is enabled, add your credentials:
mongodb://username:password@localhost:27017/mydb2MongoDB Atlas
- Go to MongoDB Atlas
- Select your cluster and click Connect
- Choose Connect your application
- Copy the connection string
- Replace
<password>with your database password - Replace
myFirstDatabasewith your database name
mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/mydb?retryWrites=true&w=majority3Self-Hosted MongoDB
For a MongoDB server hosted on your own infrastructure:
mongodb://username:password@your-server.com:27017/mydbConnect in Agent M
- Open Agent M
- Create a new connection
- Select MongoDB as the database type
- Enter a name for your connection
- Paste your connection string
- Click Connect
Common Connection Parameters
| Parameter | Description |
|---|---|
| authSource | Specify the database used for authentication |
| retryWrites | Enable retryable writes (true/false) |
| w | Write concern (majority, 0, 1, 2, etc.) |
| ssl | Enable SSL/TLS connection (true/false) |
Troubleshooting
Authentication Failed
- Verify username and password are correct
- Check if the authentication database is specified correctly
- Ensure URL-encoded special characters in password
Connection Timeout
- Check if MongoDB is running
- Verify firewall allows port 27017
- For Atlas, check IP whitelist settings
Network Issues
- Verify host address is correct
- Check internet connection (for Atlas/cloud)