MongoDB
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:
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=majorityPaste 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:
- 1Inspects the collection schemas by sampling documents.
- 2Translates your question into a MongoDB aggregation pipeline.
- 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:
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
$lookupstages (the AI can generate these for simple joins). - Schema inference works best when documents in a collection share a consistent structure.