Navigation

MongoDB Shell (mongosh)

On this page

The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 14.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database.

mongosh is available as a standalone package in the MongoDB download center.

To learn how to download and install the mongosh binary, see Install mongosh.

Once you have installed the MongoDB Shell and added it to your system PATH, you can connect to a MongoDB deployment. To learn more, see Connect to a Deployment.

Run the .editor command within the MongoDB Shell to open the editor and manage comprehensive multiline functions:

Example

Run the following command within the MongoDB Shell:

.editor

The MongoDB Shell enters editor mode:

// Entering editor mode (^D to finish, ^C to cancel)
const { Writable } = require('stream');
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
}
});
  • Press Cmd/Ctrl + d to finish and run your function.
  • Press Cmd/Ctrl + c to cancel and exit without running your function.

When you close the editor, the MongoDB Shell does not save any functions you had entered into the editor.

The new MongoDB Shell, mongosh, offers numerous advantages over the legacy mongo shell, such as:

  • Improved syntax highlighting.
  • Improved command history.
  • Improved logging.

Currently mongosh supports a subset of the mongo shell methods. Achieving feature parity between mongosh and the mongo shell is an ongoing effort.

To maintain backwards compatibility, the methods that mongosh supports use the same syntax as the corresponding methods in the mongo shell. To see the complete list of methods supported by mongosh, see MongoDB Shell Methods.

Give Feedback

On this page