> ## Documentation Index
> Fetch the complete documentation index at: https://ngquct-feat-ai-sql-walkthroughs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB

> Connect to MongoDB with MQL shell queries, collection browsing, and automatic Atlas SRV setup

# MongoDB Connections

Collections appear as tables in the sidebar. Documents display with top-level fields as columns and nested objects as formatted JSON. MongoDB is a registry plugin: it auto-installs when you create a MongoDB connection, or install it from **Settings** > **Plugins** > **Browse** > **MongoDB Driver**.

## Quick Setup

<Steps>
  <Step title="Create Connection">
    Click **New Connection**, select **MongoDB**, enter hosts and credentials, and click **Create**
  </Step>

  <Step title="Test Connection">
    Click **Test Connection** to verify
  </Step>
</Steps>

## Connection Settings

| Field              | Default           | Notes                                                                        |
| ------------------ | ----------------- | ---------------------------------------------------------------------------- |
| **Hosts**          | `localhost:27017` | Add multiple `host:port` pairs for replica sets                              |
| **Database**       | -                 | Optional. Leave empty to browse all databases. Switch with **Cmd+K**         |
| **Username**       | -                 | Leave empty for local dev without auth                                       |
| **Password**       | -                 |                                                                              |
| **Auth Mechanism** | Default           | SCRAM-SHA-1, SCRAM-SHA-256, X.509, or AWS IAM. In the Authentication section |

**Advanced**: **Auth Database** (usually `admin`), **Read Preference**, **Write Concern**, **Use SRV Record**, and **Replica Set** name.

<Frame caption="MongoDB connection form">
  <img className="block dark:hidden" src="https://mintcdn.com/ngquct-feat-ai-sql-walkthroughs/Bj1EoTflf_jG6N_3/images/mongodb-connection-form.png?fit=max&auto=format&n=Bj1EoTflf_jG6N_3&q=85&s=22081717d737f2fc0c5ea63f12e05bb3" alt="MongoDB connection form with the multi-host Hosts editor" width="1560" height="960" data-path="images/mongodb-connection-form.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/ngquct-feat-ai-sql-walkthroughs/Bj1EoTflf_jG6N_3/images/mongodb-connection-form-dark.png?fit=max&auto=format&n=Bj1EoTflf_jG6N_3&q=85&s=850229bd9c5167d38f30c0a846a0d376" alt="MongoDB connection form with the multi-host Hosts editor" width="1560" height="960" data-path="images/mongodb-connection-form-dark.png" />
</Frame>

## MongoDB Atlas (SRV)

The **Use SRV Record** toggle in Advanced connects with the `mongodb+srv://` scheme. For hosts ending in `.mongodb.net`, TablePro enables SRV automatically and turns TLS on if the SSL mode is Disabled, since Atlas requires both. An Atlas connection needs only the cluster hostname, username, and password.

## Replica Sets

The **Hosts** field accepts multiple `host:port` pairs separated by commas (for example `host1:27017,host2:27017,host3:27017`). TablePro discovers the primary automatically and routes writes there. Set the replica set name in **Advanced**.

You can also paste a multi-host URI directly:

```
mongodb://user:pass@host1:27017,host2:27017,host3:27017/db?replicaSet=rs0
```

<Note>
  SSH tunneling only forwards the first host. Other members must be reachable from the SSH server.
</Note>

## SSL/TLS

The MongoDB driver has no TLS fallback. **Preferred** behaves the same as **Required** (the SSL pane shows a warning). For unencrypted local instances, use **Disabled** or [SSH tunneling](/databases/ssh-tunneling). See [SSL/TLS](/features/ssl) for details.

## Connection URL

```text theme={null}
mongodb://user:password@host:27017/database?authSource=admin
mongodb+srv://user:password@cluster.mongodb.net/database
```

The `mongodb+srv://` scheme resolves hosts through DNS SRV records and does not allow a port. If you paste an SRV URL that includes one (for example `cluster.mongodb.net:27017`), TablePro strips it before connecting. The plain `mongodb://` scheme keeps any port you provide.

See [Connection URL Reference](/databases/connection-urls) for all parameters.

## Features

**Collection Browsing**: Sidebar shows all collections. Click to view documents in the data grid. Top-level fields render as columns, nested objects and arrays as formatted JSON, ObjectIds as strings. The grid schema is inferred by sampling documents.

**Views**: **New View** in the sidebar opens a query tab with a `db.createView("view_name", "source_collection", [pipeline])` template. Editing a view definition pre-fills a `db.runCommand({"collMod": ...})` command.

**Explain**: Press `Cmd+Option+E` on an MQL statement. TablePro converts `find`, `aggregate`, `countDocuments`, update, delete, and `findOneAnd*` calls into `db.runCommand({"explain": ..., "verbosity": "executionStats"})`.

**MQL Shell Queries**:

```javascript theme={null}
// Find documents with filtering
db.users.find({ age: { $gte: 18 }, active: true })

// Find with projection and sorting
db.orders.find(
  { status: "completed" },
  { customerId: 1, total: 1, date: 1 }
).sort({ date: -1 }).limit(20)

// Aggregation pipeline (dates use Extended JSON)
db.sales.aggregate([
  { "$match": { "date": { "$gte": { "$date": "2025-01-01T00:00:00Z" } } } },
  { "$group": { "_id": "$product", "totalSales": { "$sum": "$amount" } } },
  { "$sort": { "totalSales": -1 } },
  { "$limit": 10 }
])

// Count documents
db.users.countDocuments({ role: "admin" })
```

Filters and pipelines are parsed as JSON/Extended JSON, not JavaScript. mongosh helpers like `ISODate("2025-01-01")` fail to parse; write `{"$date": "2025-01-01T00:00:00Z"}` instead.

**Supported methods**: collection-level `find`, `findOne`, `aggregate`, `countDocuments`/`count`, `insertOne`/`insertMany`, `updateOne`/`updateMany`, `replaceOne`, `deleteOne`/`deleteMany`, `findOneAndUpdate`/`findOneAndReplace`/`findOneAndDelete`, `createIndex`, `dropIndex`, `drop`; database-level `getCollectionNames`/`listCollections`, `createCollection`, `dropDatabase`, `version`, `stats`. Anything else goes through `db.runCommand({...})` or `db.adminCommand({...})`. Unlisted shell methods (for example `distinct` or `getUsers`) return an unsupported-method error.

## Troubleshooting

**Connection refused**: Check MongoDB is running (`brew services start mongodb-community`), verify host/port in `mongod.conf`, check `bindIp` setting.

**Auth failed**: Verify username, password, auth database, and auth mechanism. The MQL editor does not parse `db.getUsers()` or `db.createUser()`; inspect users with `db.runCommand({"usersInfo": 1})` or manage them in mongosh.

**Timeout**: Verify host/port, check network and firewall, whitelist your IP in MongoDB Atlas.

**Limitations**: Transactions are not exposed (statements always run standalone, on any topology), schema is inferred by sampling, GridFS is not browsable, change streams are unsupported.
