How to Use MongoDB Shell on Ubuntu 22.04

MongoDB Shell is a powerful command-line interface that allows users to interact with MongoDB databases. It provides a convenient way to query, update, and manage data within MongoDB collections using JavaScript-like syntax. MongoDB databases, on the other hand, are document-oriented NoSQL databases that offer flexible schema design and scalability, making them ideal for handling large amounts of structured or semi-structured data.

We will guide you in this tutorial on how to use the MongoDB Shell effectively on Ubuntu 22.04. Read this article to learn how to install MongoDB on Ubuntu 22.04. You can expect to gain a concise understanding of the fundamental commands available in the Mongo shell and how to use them efficiently.

Working with Mongo Shell on Ubuntu 22.04

Open the terminal and display the MongoDB version using the below-mentioned command:

$ monogdb --version

Now, access MongoDB Shell. To launch the MongoDB Shell, use this command:

$ mongosh

You will now see the MongoDB Shell prompt where you can execute various commands.

Basic MongoDB Shell Commands

Here are some essential commands to get you started with the MongoDB Shell:

List databases using MongoDB Shell

To list all MongoDB databases on the terminal, use this command:

> show dbs

The above command will display all MongoDB databases on the terminal.

Switch to a specific database

If you want to use a specific MongoDB database, follow this syntax:

> use <database-name>
> use customer

The above command creates a new database with the name ‘customer’ if it doesn’t exist. Otherwise, it will show the existing database. In Mongo shell, you can use this to create a new database.

Create Collection in MongoDB

In MongoDB, a database contains different collections. To create a collection in the MongoDB database, use this syntax:

>  db.createCollection(<collectionName>)

In the above command, Replace <collectionName> with the desired name for your collection.

For example, we want to create a collection of “orders” inside the ‘customer’ database. To do this, select customer data and create a collection using these commands:

> use customer
> db.createCollection(“orders”)

Show Collections

To list the database collections, use the following command:

> show collections

The above command will display all the collections in the current database.

Insert one Document in the Collection

To insert a new document into the collection, use this command:

> db.orders.insertOne({name: ‘Samreena’})

The above command will use the ‘insertOne’ operation to store a new document containing an object {name: ‘Samreena’} in MongoDB ‘orders’ collection. Now, if you want to retrieve all documents from the specified collection, use this command:

> db.collectionName.find()

The above are all the basic MongoShell commands for dealing with MongoDB databases.


In this guide, we have learned how to install and use the MongoDB Shell on Ubuntu 22.04. We covered the installation process and provided an overview of essential MongoDB Shell commands. Following this guide, you can now interact with MongoDB databases using the command-line interface. Explore the official MongoDB documentation for more advanced features and command options.

Print Friendly, PDF & Email