MongoDB Shell (mongosh)
mongosh is the official command-line interface (CLI) for interacting with MongoDB deployments. It provides a modern JavaScript-based REPL (Read-Eval-Print Loop) for database administration, data manipulation, and development. The npm package `mongosh` acts as a convenience wrapper, primarily distributing the compiled `mongosh` binary rather than a JavaScript library for programmatic import. The current stable version is 2.8.2. New versions are released frequently, often coinciding with MongoDB server updates, Node.js runtime upgrades (e.g., moving to Node.js 24 in v2.7.0), and regular bug fixes and minor feature additions. It differentiates itself by being the officially supported, feature-rich, and actively maintained interactive shell for MongoDB, offering advanced features like schema sampling, streaming processor attributes, and improved authentication mechanisms compared to older MongoDB shells.
Common errors
-
mongosh: command not found
cause The `mongosh` package was either not installed globally, or the npm global bin directory is not in your system's PATH.fixEnsure `mongosh` is installed globally with `npm install -g mongosh` and verify that your npm global bin path is correctly configured in your shell's PATH environment variable. -
Error: command not found: db
cause This error occurs when attempting to use MongoDB shell commands (like `db.collection(...)`) in a standard Node.js environment or directly in your system's shell, outside of the `mongosh` REPL.fixRun your MongoDB commands within the `mongosh` REPL after connecting to a database, or use the official `mongodb` driver for Node.js applications if you need programmatic interaction. -
Failed to connect to <address> authentication failed
cause Incorrect username, password, authentication database, or authentication mechanism provided when connecting to a MongoDB instance that requires authentication.fixVerify your `--username`, `--password`, `--authenticationDatabase`, and `--authenticationMechanism` options. Ensure the user has the necessary permissions on the specified database. -
Error: Node.js version <X> is not supported by mongosh.
cause Although `mongosh` bundles its own Node.js runtime, the `npm install` process itself requires a compatible system Node.js version. If your system's Node.js is too old, the installation may fail.fixUpgrade your system's Node.js version to meet the `mongosh` npm package's `engines` requirement (e.g., `>=16.15.0`). You can use a tool like `nvm` to manage Node.js versions.
Warnings
- breaking Starting with v2.6.0, `mongosh` outputs full BSON objects by default for certain operations. This change might affect scripts or tools that parse `mongosh` output and rely on previous, potentially abbreviated, representations.
- deprecated The option-based `explain` API was deprecated in v2.7.0. While still functional for now, users should transition to the updated `explain` methods to ensure future compatibility and avoid potential breaking changes in later versions.
- gotcha The `mongosh` npm package is a distribution wrapper for the `mongosh` CLI binary and bundles its own Node.js runtime (e.g., Node.js 24 since v2.7.0). Therefore, the system Node.js version is primarily relevant for the `npm install` process (requiring `>=16.15.0`), but not for the `mongosh` runtime itself. It is not intended for direct JavaScript `import` or `require` into Node.js applications.
- gotcha For production environments or when requiring a fully supported `mongosh` binary with specific enterprise features or support, MongoDB recommends downloading `mongosh` directly from the official MongoDB website (https://www.mongodb.com/try/download/shell) rather than relying solely on the npm distribution. The npm package is primarily for ease of installation and development workflows.
Install
-
npm install mongosh -
yarn add mongosh -
pnpm add mongosh
Imports
- mongosh (main package)
import mongosh from 'mongosh'; const mongosh = require('mongosh');// Not designed for direct JavaScript import. Install globally via npm and run from terminal.
- db, ObjectId, BSON (MongoDB shell API objects)
import { db, ObjectId } from 'mongosh'; const { BSON } = require('mongosh');// These objects are available within the mongosh REPL environment, not via Node.js imports. Use `mongodb` driver for Node.js applications.
- spawn (for programmatic CLI execution)
import { executeMongosh } from 'mongosh'; // No such exported functionimport { spawn } from 'child_process';
Quickstart
// This quickstart demonstrates using the mongosh CLI, not programmatic Node.js imports.
// First, ensure mongosh is installed globally via npm:
// npm install -g mongosh
// From your terminal, connect to a local MongoDB instance and insert a document:
mongosh "mongodb://localhost:27017/testdb" \
--eval "db.collection('mycollection').insertOne({ item: 'notebook', qty: 50, tags: ['stationery', 'office'] })"
// Then, query the inserted document using --json for structured output:
mongosh "mongodb://localhost:27017/testdb" \
--eval "db.collection('mycollection').find({ item: 'notebook' }).toArray()" \
--json=relaxed
// For scripting, you can place shell commands into a JavaScript file (e.g., my_script.js):
// // my_script.js
// const conn = new Mongo('mongodb://localhost:27017/anotherdb');
// const db = conn.getDB('anotherdb');
// db.collection('documents').insertOne({ date: new Date(), message: 'Hello from mongosh script!' });
// printjson(db.collection('documents').findOne());
// And execute it via the CLI:
// mongosh my_script.js