Skip to content

mongosh

383 stars
official open-source Has MCP macOS Cross-platform Linux Actively maintained

The official MongoDB Shell. Query documents, run aggregation pipelines, manage collections, and script database operations with native JSON output.

mongosh has both a CLI and an MCP server. See when to use each

Part of the MongoDB CLI tools for AI agents

What your agent can do

Your MongoDB queries are slow. You suspect a missing index, but the Compass GUI shows you a visual execution plan that's hard to compare across queries. Your agent runs `mongosh --quiet --eval "db.orders.find({customer_id: 42}).explain('executionStats')"` and gets the exact scan type, documents examined, and execution time as structured JSON. Missing index identified in seconds.MongoDB is document-native, and mongosh speaks its language. Every result is already JSON. No format conversion needed. Your agent runs `mongosh --json=relaxed --eval "db.products.find({category: 'electronics'}).toArray()"` and gets a JSON array ready to pipe into `jq`, write to a file, or feed into another tool. This is mongosh's structural advantage over relational database CLIs that return tabular data by default.Aggregation pipelines are MongoDB's most powerful feature and they're fully scriptable. Your agent builds multi-stage pipelines: `$match` to filter, `$group` to aggregate, `$lookup` for joins, `$project` to reshape output. Each stage is a JSON object. Your agent constructs the pipeline programmatically, runs it through `--eval`, and gets the results as JSON. No GUI pipeline builder needed.mongosh is a full Node.js REPL under the hood. Your agent can use `require()`, `async/await`, `for...of` loops, and any JavaScript expression. This means complex multi-step operations — seeding test data, running data migrations, generating reports — work as JavaScript scripts passed via `--file`. Not just SQL statements, but actual programs that happen to talk to a database.The `--quiet` flag is the agent-native essential. Without it, mongosh prints connection banners, warnings about deprecated drivers, and MongoDB version info. With `--quiet`, output is just the query result. Combined with `--json=relaxed`, your agent gets clean, parseable JSON with no noise.

Limitations

MongoDB-specific with no support for relational databases. The `--json` output mode wraps some values in Extended JSON format (`{"$oid": "..."}`, `{"$date": "..."}`), which downstream tools may not expect. Low GitHub stars (383) because mongosh ships bundled with MongoDB installers and Compass — adoption is much higher than stars suggest. Some administrative operations require `mongod` or Atlas CLI.

Key Commands

mongosh --eval Execute a JavaScript expression against the database and return the result
mongosh --json=relaxed Output results as relaxed Extended JSON for human-readable structured output
db.collection.find() Query documents matching a filter with optional projection
db.collection.aggregate() Run an aggregation pipeline for complex data transformations
db.collection.insertMany() Insert multiple documents in a single operation
show dbs List all databases with their sizes
db.collection.createIndex() Create an index on a collection for query optimization

GitHub Stats

repo mongodb-js/mongosh
stars 383
language TypeScript
license Apache-2.0
last commit Mar 22, 2026

FAQ

Is mongosh free?
Yes. mongosh is open-source under the Apache 2.0 license. Install with `brew install mongosh` on macOS, `npm install -g mongosh` for any platform, or download from mongodb.com. It connects to MongoDB Community (free), MongoDB Atlas (free tier available), and any MongoDB-compatible service.
Can AI agents use mongosh?
Yes. mongosh runs fully non-interactive with `--eval` for single expressions and `--file` for JavaScript scripts. The `--json=relaxed` flag outputs structured JSON. The `--quiet` flag suppresses connection banners for clean machine-readable output. MongoDB also has an official MCP server (mongodb-js/mongodb-mcp-server, 972 stars) that gives agents natural-language access to database operations.
What is the difference between mongosh and the old mongo shell?
mongosh replaced the legacy `mongo` shell in MongoDB 5.0+. Key differences: mongosh is a full Node.js REPL (supports `async/await`, `require()`, modern JavaScript), has `--json` output for structured data, syntax highlighting, better error messages, and auto-completion. The legacy shell used SpiderMonkey (Mozilla's engine). If you see `mongo` in older docs, use `mongosh` instead — same commands, better runtime.
How do you connect mongosh to MongoDB Atlas?
Copy your connection string from the Atlas dashboard (Database → Connect → Shell). Run `mongosh 'mongodb+srv://user:pass@cluster.mongodb.net/mydb'`. mongosh handles TLS, authentication, and replica set discovery automatically. Your agent stores the connection string in an environment variable and connects with `mongosh $MONGODB_URI --quiet --json=relaxed`.
Does mongosh support JSON output?
Yes, natively. `--json=relaxed` outputs human-readable JSON. `--json=canonical` outputs strict Extended JSON (preserves all BSON types). Inside scripts, `EJSON.stringify()` and `printjson()` format any value as JSON. MongoDB stores documents as BSON (binary JSON), so query results are inherently JSON-shaped. This is mongosh's biggest advantage over relational CLIs for agent workflows.

Last verified: Mar 25, 2026