Open In App

How To Use the MongoDB Shell

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB Shell, the gateway to MongoDB’s powerful database management system, offers possibilities for developers and administrators. From seamlessly connecting to your MongoDB server to executing complex database operations easily. In this article, We will learn about How To use the MongoDB Shell by understanding all the required steps and so on.

How to start MongoDB Using Shell?

The MongoDB Shell (mongosh) is a REPL (Read-Eval-Print Loop) environment. It’s like a command-line interface where we can execute MongoDB commands and interact with the database allowing us to perform administrative tasks and read, write or manipulate data directly.

Below are the steps to start the MongoDB using Shell are as follows:

  1. Connecting to MongoDB Server
  2. Executing Commands
  3. Getting Interactive Help from the Shell

Let’s understand step by step:

Step 1: Connecting to MongoDB Server

First of all we will open our terminal and type mongosh (for versions 7.0+) to launch the MongoDB shell. This connects us to the default MongoDB server running on our local machine.

Connecting-to-MongoDB-Server

Output after ‘mongosh’ command

Step 2: Executing Commands

Like any other command-line interface, we can enter MongoDB commands directly in the shell. For example, to list all databases, type show dbs.

Executing-Commands

displays all databases present locally

Step 3: Getting Interactive Help from the Shell

The MongoDB Shell commands provide essential functionality for interacting with MongoDB databases. Commands like use and show allow us to switch databases and display information about databases and collections. exit and quit are used to exit the shell.

The Mongo and connect commands are used to create new connections to MongoDB servers. The load command loads and runs JavaScript files in the shell environment. Overall, these commands streamline database management tasks and enhance the MongoDB Shell’s usability.

MongoDB shell have in-built help system that we can use to get information on database system’s available commands. The help screen can be accessed by using help command.

Output:

  Shell Help:

use Set current database
show 'show databases'/'show dbs': Print a list of all available databases.
'show collections'/'show tables': Print a list of all collections for current database.
'show profile': Prints system.profile information.
'show users': Print a list of all users for current database.
'show roles': Print a list of all roles for current database.
'show log <type>': log for current connection, if type is not set uses 'global'
'show logs': Print all logs.

exit Quit the MongoDB shell with exit/exit()/.exit
quit Quit the MongoDB shell with quit/quit()
Mongo Create a new connection and return the Mongo object. Usage: new Mongo(URI, options [optional])
connect Create a new connection and return the Database object. Usage: connect(URI, username [optional], password [optional])
it result of the last line evaluated; use to further iterate
version Shell version
load Loads and runs a JavaScript file into the current shell environment
enableTelemetry Enables collection of anonymous usage data to improve the mongosh CLI
disableTelemetry Disables collection of anonymous usage data to improve the mongosh CLI
passwordPrompt Prompts the user for a password
sleep Sleep for the specified number of milliseconds
print Prints the contents of an object to the output
printjson Alias for print()
convertShardKeyToHashed Returns the hashed value for the input using the same hashing function as a hashed index.
cls Clears the screen like console.clear()
isInteractive Returns whether the shell will enter or has entered interactive mode

The MongoDB shell will return a list of more help entries we can use to learn about, here we’ll learn some of the most commonly used commands from the shell:

use: It allows us to switch between databases in the MongoDB shell. For example, if we have multiple databases in our MongoDB instance, we can use the use command followed by the name of the database we want to switch to.

Example:

use admin

Output:

Getting-Interactive-Help-from-the-Shell

the admin db is selected

show: Depending on the context, show can be used to display databases, collections, users, roles, etc.

Example:

show databases

Output:

show

displaying all the database present

exit: Allows us to exit the MongoDB shell session. We can also use exit, exit(), or .exit to quit the MongoDB shell.

Conclusion

Overall, MongoDB Shell offers a seamless way to connect to MongoDB servers, execute commands, and perform administrative tasks. The Shell’s interactive help system provides valuable information on available commands, making it easier for developers and administrators to work with MongoDB databases. By following the steps defined in this article users can understand the power of MongoDB Shell to streamline their database management workflows and enhance their MongoDB experience



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads