Skip to content

mongosh — MongoDB CLI for AI Agents

Let your AI agent query documents, run aggregations, and manage your NoSQL database through the command line

Browse all CLI tools for AI agents

What your agent can do

Your MongoDB collection has 2 million documents and a query that should take milliseconds is taking 8 seconds. You open Compass, navigate to the collection, open the explain plan tab, paste the query, wait for the visual tree to render. Your agent runs `mongosh --quiet --eval "db.orders.find({customer_id: 42}).explain('executionStats')"` and reads the execution stats as JSON. Collection scan instead of index scan. `db.orders.createIndex({customer_id: 1})`. Problem solved.mongosh outputs JSON natively because MongoDB stores JSON natively. This is the fundamental difference from relational database CLIs. When your agent runs a query, the result is already structured data — no format conversion, no column-to-key mapping, no parsing. `mongosh --json=relaxed --eval "db.users.find({role: 'admin'}).toArray()"` returns a JSON array of documents. Pipe to `jq`, write to a file, feed into another tool. Zero friction.Aggregation pipelines are where MongoDB shines and where agents add the most value. A pipeline chains stages — `$match` filters documents, `$group` computes totals, `$lookup` joins collections, `$project` reshapes output. Each stage is a JSON object. Your agent constructs complex multi-stage pipelines programmatically, runs them through `--eval`, and processes the results. Writing a 5-stage aggregation pipeline in a GUI is painful. Writing it as JSON that your agent can generate, test, and iterate on is natural.mongosh is a full Node.js REPL, not just a query runner. Your agent can write JavaScript programs that connect to MongoDB: `for...of` loops over collections, `async/await` for async operations, `require()` for external modules. Pass scripts with `--file` for complex operations like data migrations, test seeding, or report generation. It's a programming environment that happens to have database access.The `--quiet` flag removes connection banners and version warnings. Combined with `--json=relaxed`, output is clean structured data with no noise. This is what separates interactive use from agent-native operation. Your agent never sees "Connecting to: mongodb://..." or "Using MongoDB: 7.0.x" — just the data it asked for.

Frequently asked questions

Can AI agents manage MongoDB with the CLI?
Yes. mongosh runs non-interactive with `--eval` for single expressions and `--file` for scripts. The `--json=relaxed` flag outputs structured JSON, and `--quiet` suppresses connection noise. Since MongoDB stores documents as JSON, results are already structured — no format conversion needed. MongoDB also has an official MCP server (mongodb-js/mongodb-mcp-server, 972 stars) for agent-native database access. Install with `brew install mongosh` or `npm install -g mongosh`.
What is the difference between mongosh and psql?
Different databases, different strengths. psql connects to PostgreSQL (relational, tables, SQL). mongosh connects to MongoDB (document store, collections, JavaScript). The key difference for agents: MongoDB results are natively JSON — no format conversion needed. PostgreSQL requires `\pset format json` or `row_to_json()`. Choose based on your database, not the CLI. If your data is relational with strict schemas, PostgreSQL. If it's document-shaped with flexible schemas, MongoDB.
Does mongosh work with MongoDB Atlas?
Yes. Copy your Atlas connection string and run `mongosh 'mongodb+srv://user:pass@cluster.mongodb.net/db'`. mongosh handles TLS, authentication, and replica set routing automatically. All commands work identically against Atlas, self-hosted MongoDB, and Docker containers. Your agent stores the connection string in `MONGODB_URI` and connects to any environment with `mongosh $MONGODB_URI --quiet --json=relaxed`.