---
title: "MongoDB - Documentation"
description: "Connect MongoDB and MongoDB Atlas. Learn how AI for Database translates natural language into aggregation pipelines."
canonical: "https://www.aifordatabase.com/docs/mongodb/"
---

# MongoDB

intermediate 6 min read

## Connecting MongoDB

AI for Database supports MongoDB 4.4+ and MongoDB Atlas. Because MongoDB uses a document model instead of tables and rows, the AI translates your questions into aggregation pipelines rather than SQL.

### Create a Read-Only User

In the MongoDB shell or Atlas UI:

```javascript
use admin
db.createUser({
  user: "aifordb_reader",
  pwd: "a-strong-random-password",
  roles: [
    { role: "read", db: "your_database" }
  ]
})
```

For Atlas, you can create the user in the **Database Access** section of the Atlas dashboard. Assign the built-in `readAnyDatabase` role if you want the AI to query across multiple databases.

### Connection String

AI for Database accepts a standard MongoDB connection string:

```
mongodb+srv://aifordb_reader:password@cluster0.example.mongodb.net/your_database?retryWrites=true&w=majority
```

Paste this into the **Connection String** field when adding a MongoDB connection.

### How Queries Work

When you ask a question against a MongoDB database, AI for Database:

1.  1Inspects the collection schemas by sampling documents.
2.  2Translates your question into a MongoDB aggregation pipeline.
3.  3Runs the pipeline and returns results as a table or chart.

For example, asking "What is the average order value by country?" generates a pipeline like:

```javascript
db.orders.aggregate([
  { $group: { _id: "$country", avgValue: { $avg: "$total" } } },
  { $sort: { avgValue: -1 } }
])
```

You can view and edit the generated pipeline by clicking **View Query**.

### Atlas Configuration

For MongoDB Atlas, ensure your AI for Database IP address is added to the **Network Access** allowlist. If you are using our cloud product, add the IP ranges listed in **Settings > Connection Info**.

For self-hosted deployments, no allowlisting is needed since the connection originates from your own infrastructure.

### Limitations

*   MongoDB connections do not support joins across collections in a single query. Ask about one collection at a time, or use `$lookup` stages (the AI can generate these for simple joins).
*   Schema inference works best when documents in a collection share a consistent structure.

Source: https://www.aifordatabase.com/docs/mongodb/
