> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloudthinker.io/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB

> Connect MongoDB or Atlas so agents can inspect databases, collections, indexes, and query plans in read-only mode

Connect a MongoDB deployment or MongoDB Atlas cluster to let CloudThinker agents inspect databases, collections, indexes, and query plans. You give CloudThinker one connection string for a dedicated database user, and the connection always runs in read-only mode: agents can query and explain, but can't insert, update, drop, or create indexes.

## Prerequisites

* A MongoDB deployment or Atlas cluster CloudThinker can reach from the internet. Agents connect from CloudThinker Cloud, so `localhost` and private addresses resolve there, not on your network. Atlas private endpoints and VPC peering aren't supported. See [Reaching private services](https://docs.cloudthinker.io/guide/connections/overview.md#reaching-private-services).
* Permission to create a database user: `userAdmin` on `admin` for self-hosted MongoDB, or **Project Database Access Admin** (or higher) in Atlas.

## Setup

**1. Create a read-only user**

> [tab] Self-hosted MongoDB
>
> Connect with `mongosh` as an admin, then create the user on the `admin` database:
>
> ```javascript theme={null}
> use admin
> db.createUser({
>   user: "cloudthinker_readonly",
>   pwd: "<secure-password>",
>   roles: [
>     { role: "readAnyDatabase", db: "admin" },  // or { role: "read", db: "shop" } for one database
>     { role: "clusterMonitor", db: "admin" }
>   ]
> })
> ```

> [tab] MongoDB Atlas
>
> In Atlas, open **Security → Database Access** and click **Add New Database User**. Choose **Password** authentication, enter `cloudthinker_readonly` and a strong password, and under **Database User Privileges** pick the built-in role **Only read any database**. To limit agents to one database, add `read@shop` under **Specific Privileges** instead. Click **Add User**.
>
> For server status, logs, and replica lag, also add the **clusterMonitor** role under **Specific Privileges**.

**2. Open network access**

Allow CloudThinker's egress IPs to reach the deployment. These docs don't publish CloudThinker's egress IP list: ask your CloudThinker contact, or [contact us](https://cloudthinker.io/contact). For self-hosted MongoDB, allow inbound connections on port `27017` in your firewall or security group and make sure `bindIp` in `mongod.conf` isn't only `127.0.0.1`. In Atlas, add one entry per IP under **Security → Network Access → IP Access List**.

**3. Build the connection string**

> [tab] Self-hosted MongoDB
>
> For a replica set, list every member and name the set:
>
> ```text theme={null}
> mongodb://cloudthinker_readonly:<secure-password>@h1.example.com:27017,h2.example.com:27017,h3.example.com:27017/?replicaSet=rs0&authSource=admin&tls=true&readPreference=secondaryPreferred&appName=CloudThinker
> ```
>
> For a single server, use one host and drop `replicaSet`. Drop `tls=true` only if the server doesn't support TLS.

> [tab] MongoDB Atlas
>
> On your cluster, click **Connect**, choose **Drivers**, and copy the `mongodb+srv://` string. Put in the user and password from step 1:
>
> ```text theme={null}
> mongodb+srv://cloudthinker_readonly:<secure-password>@cluster0.abcde.mongodb.net/?readPreference=secondaryPreferred&appName=CloudThinker
> ```
>
> Atlas turns on TLS for `mongodb+srv://` strings automatically.

`readPreference=secondaryPreferred` sends agent reads to secondaries, so they stay off your production primary. It falls back to the primary when no secondary is available. `appName=CloudThinker` labels the connection in your MongoDB logs. URL-encode special characters in the password (`@` becomes `%40`).

**4. Add the connection in CloudThinker**

Open **More → Workspace settings → Connections**, select **MongoDB**, and click **Connect** on its detail panel. In the dialog, optionally change the **Alias** and **Description**. Paste the string into the connection string field, labeled **MDB\_MCP\_CONNECTION\_STRING**, and click **Connect**.

**Success state:** the connection shows **Connecting** and **Verifying**, then **Connected**. CloudThinker then runs [Discovery](https://docs.cloudthinker.io/guide/connections/overview.md#what-happens-after-you-connect) once: a read-only look at your databases and collections whose results show in the **Discovery** section of the connection's detail panel.

## Connection details

| Field | Required | Description | Example |
| - | - | - | - |
| **Alias** | No | Short name for this deployment. Prefilled with the next free alias, such as `mongodb-1` | `prod-atlas` |
| **Description** | No | What this deployment is. Agents read it to choose between instances | `Production Atlas cluster` |
| **MDB\_MCP\_CONNECTION\_STRING** | Yes | Full MongoDB URI with user, password, host, and options | `mongodb+srv://cloudthinker_readonly:<password>@cluster0.abcde.mongodb.net/` |

Useful options in the string:

| Option | Use it when |
| - | - |
| `authSource=admin` | The user lives on the `admin` database and the string names a different database |
| `tls=true` | The server supports TLS and the scheme is `mongodb://` |
| `readPreference=secondaryPreferred` | Recommended for production: agent reads go to secondaries and stay off the primary |
| `appName=CloudThinker` | You want to spot CloudThinker sessions in logs and `currentOp` |

## Required permissions

| Role | What it unlocks |
| - | - |
| `readAnyDatabase` | List databases and collections, sample documents, run `find`, `aggregate`, and `count`, and read indexes and query plans |
| `clusterMonitor` | Server status, replica set status and lag, and recent server logs |

> [!TIP]
>
> To limit agents to one database, grant `read` on that database instead of `readAnyDatabase`, as shown in step 1. Never grant `readWrite`, `dbAdmin`, or `root`: the connection is read-only anyway, so extra roles add risk without adding capability.

## Agent capabilities

| Capability | What agents can do |
| - | - |
| **Inventory** | List databases and collections with document counts and storage size |
| **Schema** | Infer field names and types from sampled documents |
| **Queries** | Run `find`, `aggregate`, and `count` queries to answer questions about your data |
| **Index review** | List indexes per collection and check whether a query uses one with `explain` |
| **Server health** | Read server logs, connections, and replica status (needs `clusterMonitor`) |

Agents can't write. Insert, update, delete, drop, and index-creation tools are switched off for this connection, so a request like "drop the sessions collection" can't run. Agents can still recommend an index for you to create.

Schema inference and queries return real documents, which can include personal data. [Guardrails](https://docs.cloudthinker.io/guide/security/custom-guardrails.md) decide which identifiers agents see only as placeholders. Email and phone are on by default.

### Verify the connection

```text theme={null}
List the MongoDB databases with their collection counts and storage size
```

Expect each database name with its collections and size. On Atlas, the `local` and `config` system databases are skipped because Atlas blocks access to them.

### Example prompts

```text theme={null}
Which queries on the orders collection scan the whole collection, and what index would fix them?
Show the indexes on every collection in the shop database and flag ones that look redundant
What does a typical document in users look like? List its fields and types
```

## Troubleshooting

> [accordion] Authentication failed
>
> The user, password, or auth database is wrong. If you created the user on `admin` and the string names another database, add `authSource=admin`. In Atlas, check the user exists under **Database Access** and that special characters in the password are URL-encoded.

> [accordion] Server selection timed out, or connection refused
>
> CloudThinker can't reach the deployment. In Atlas, check the **IP Access List**. For self-hosted MongoDB, check your firewall and that `bindIp` isn't only `127.0.0.1`. A deployment on a private network needs a reachable endpoint first.

> [accordion] not authorized on admin to execute command (serverStatus, replSetGetStatus, getLog)
>
> The user is missing `clusterMonitor`. Add it on the `admin` database, then ask the agent again. Queries against your data keep working without it.

> [accordion] An agent says a write is blocked by read-only mode
>
> This is expected. The MongoDB connection is read-only. Make the change yourself, or ask the agent for the exact command to run.

## Security

* **Least privilege** — grant only the permissions the agents need for your use case; start read-only and widen later.
* **Read-only by default** — use read-only credentials unless you want agents to make changes through this connection.
* **Rotate credentials** — rotate keys and tokens on your normal schedule; CloudThinker picks up the new value when you update the connection.
* **Revoke on offboarding** — remove the credential at the provider when you delete a connection or a teammate leaves.

- **Read-only on two layers**: the connection itself can't write, and a user with only `read` or `readAnyDatabase` can't either, even if the string leaks.
- **Use TLS**: use `mongodb+srv://` on Atlas, or `tls=true` on self-hosted MongoDB, so credentials and query results are encrypted in transit.

## Related

> [card] [Redis](https://docs.cloudthinker.io/guide/connections/redis.md)
>
> Connect a Redis cache with a read-only ACL user.

> [card] [Connections](https://docs.cloudthinker.io/guide/connections/overview.md)
>
> See every service you can connect and how connection status works.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.