Docs Menu

mongosh Usage

On this page

  • Switch Databases
  • Insert Data into a Collection
  • .mongoshrc.js File
  • Terminate a Running Command
  • Command Exceptions
  • Clear the mongosh Console

To run commands in mongosh, you must first connect to a MongoDB deployment.

To display the database you are using, type db:


The operation should return test, which is the default database.

To switch databases, issue the use <db> helper, as in the following example:

use <database>

To access a different database from the current database without switching your current database context, see the db.getSiblingDB() method.

To list the databases available to the user, use the helper show dbs.

To create a new database, issue the use <db> command with the database that you would like to create. For example, the following commands create both the database myNewDatabase and the collection myCollection using the insertOne() operation:

use myNewDatabase
db.myCollection.insertOne( { x: 1 } );

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

The db.myCollection.insertOne() is one of the methods available in mongosh.

  • db refers to the current database.
  • myCollection is the name of the collection.

If mongosh does not accept the name of a collection, you can use the alternative db.getCollection() syntax. For instance, if a collection name contains a space or hyphen, starts with a number, or conflicts with a built-in function:

db.getCollection("3 test").find()

For more documentation of basic MongoDB operations in mongosh, see:

On startup, mongosh checks the user's HOME directory for a JavaScript file named .mongoshrc.js. If this file is found, mongosh interprets the content of .mongoshrc.js before displaying the prompt for the first time.

If you use the shell to evaluate a JavaScript file or expression, either by using the --eval option on the command line or by specifying a .js file to mongosh, mongosh reads the .mongoshrc.js file after the JavaScript has finished processing. You can prevent .mongoshrc.js from being loaded by using the --norc option.


The legacy mongo shell used a configuation file called .mongorc.js. If mongosh detects this file on startup, and .mongoshrc.js is not present, mongosh will suggest renaming .mongorc.js to .mongoshrc.js.

The legacy .mongorc.js file will not be loaded.

To terminate a running command or query in mongosh, press Ctrl + C.

Ctrl + C terminates the process in the shell, but does not terminate the process on the MongoDB server. This behavior differs from the db.killOp() method, which terminates commands on the server.

In scenarios where the output of a command includes { ok: 0 }, mongosh throws an exception and does not return the raw output from the server.


In the legacy mongo shell, when a command's output included { ok: 0 }, the behavior differs between commands. mongosh provides consistent behavior by always throwing an exception in these scenarios.

The cls command clears the console. You can also clear the console with Ctrl + L and console.clear().

Give Feedback
© 2021 MongoDB, Inc.


  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.