MongoDB Shell (mongosh)
$ mongosh Official High Riskmongosh is the modern Node.js-based MongoDB shell replacing legacy mongo. For AI agents, --quiet combined with --eval "EJSON.stringify(...)" or --json=canonical executes queries cleanly and exits without entering the interactive REPL.
brew install mongosh mongosh --quiet --json=canonical --eval is great for deterministic shell scripts and migrations; MongoDB's official MCP server helps when exploring unfamiliar collections.
Agent-Readiness Checklist
Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.
FCRS-1.0 6-Dimension Breakdown
--quiet, --json=canonical/relaxed, and --eval enable clean JSON output and one-shot execution.
- Supports
--quiet,--eval, and--json=canonical/--json=relaxed
Read/write safety depends on MongoDB connection user roles (readPreference=secondary etc.); --eval runs arbitrary JS.
- No client-side
--readonlyflag; relies on MongoDB RBAC user permissions
Standalone binary packaged via Homebrew, apt/yum, and downloads for macOS, Linux, and Windows.
- Cross-platform standalone
mongoshdistribution
Full JavaScript/MongoDB shell expression power in a single CLI command.
- Supports aggregation pipelines,
EJSON, and admin helpers
Official mongodb-js/mongosh maintained by MongoDB under Apache-2.0.
- Official
mongodb-js/mongoshrepository
Comprehensive official documentation with flag reference.
- Official docs at mongodb.com/docs/mongodb-shell
Security, Auth & Guardrails
Connection string URI passed via $MONGODB_URI or --username / --password flags.
100% free and Apache-2.0 open-source.
# mongosh Agent Rules
- Always pass `--quiet` and `--eval "<js>"` (never start interactive `mongosh`).
- Always attach `.limit(...)` to `find()` queries and prefer `--json=canonical` or `EJSON.stringify(...)`.
- NEVER run `drop()`, `dropDatabase()`, or `deleteMany({})` without explicit approval. Agent-Ready Command Recipes
Safe Read-Only Queries
--eval mongosh "$MONGODB_URI" --quiet --json=canonical --eval "db.getCollectionNames()"
Lists database collections cleanly as canonical Extended JSON without banner noise.
limit and projection mongosh "$MONGODB_URI" --quiet --eval "EJSON.stringify(db.users.find({}, {passwordHash: 0}).limit(5).toArray())" Fetches a bounded sample while omitting sensitive fields.
Strengths for Agents
- +
--quiet --json=canonical --evalsuppresses startup banners and outputs strict Extended JSON. - + Supports full MongoDB aggregation pipelines and
EJSONhelpers inline. - + Cleanly replaces the deprecated
mongoshell.
Caveats & Pitfalls
- ! Bundled Node.js runtime gives
mongoshnoticeable cold-start latency (~0.5–1s). - ! No client-side
--readonlyflag; use a read-only database user for agent queries.