Skip to main content

MongoDB Shell Commands

caution

This is just for the sake of knowledge, you may not be using this. Just scroll, and read the document.

Happy Learning 😊

Introduction

MongoDB is a popular NoSQL database that provides a flexible and scalable solution for managing and storing data. One of the essential tools in working with MongoDB is the MongoDB Shell, which allows you to interact with the database through a command-line interface. In this doc, we will explore various MongoDB Shell commands and their responses, providing you with a comprehensive guide to get started.

Prerequisites

Before we get started, make sure you have installed MongoDB on your system. If you haven't installed MongoDB, you can follow the official documentation to install MongoDB on your system.

Let's get started

Starting MongoDB Shell

To start the MongoDB shell, run the following command in your terminal or command prompt.

The MongoDB shell is called mongosh (the older mongo shell has been replaced by it). Don't confuse it with mongod, which is the database server daemon, not the shell. You can start the MongoDB shell by running the following command in your terminal.

Start MongoDB Shell
mongosh

Show Databases

Show Databases
show dbs

It will show all the databases available in your MongoDB.

Create Database

Create Database if not exists, otherwise switch to the existing database
use <database-name>

It will create a new database with the given name if it doesn't exist, otherwise, it will switch to the existing database.

Example:

Create Database if not exists, otherwise switch to the existing database
use mydb

Here, we are creating a new database with the name mydb.

Drop Database

Drop Database
db.dropDatabase()

It will drop the current database.

Create Collection

Create Collection
db.createCollection("<collection-name>")

It will create a new collection with the given name.

Example:

Create Collection
db.createCollection("users")

Here, we are creating a new collection with the name users.

Show Collections

Show Collections
show collections

It will show all the collections available in the current database.

Drop Collection

Drop Collection
db.<collection-name>.drop()

It will drop the collection with the given name.

Example:

Drop Collection
db.users.drop()

Here, we are dropping the collection with the name users.

Insert Documents

Insert Documents
db.<collection_name>.insertOne({
<key>: <value>,
<key>: <value>,
...
})

It will insert a single document in the collection with the given name.

Example:

Insert Document in Users Collection
db.users.insertOne({
name: "Rizwan",
age: 25,
})

Here, we are inserting a single document in the collection with the name users.

Insert multiple documents
db.<collection_name>.insertMany([
{
<key>: <value>,
<key>: <value>,
...
},
{
<key>: <value>,
<key>: <value>,
...
},
...
])

It will insert multiple documents in the collection with the given name.

Find Documents

Find Documents
db.<collection_name>.find()

It will return all the documents from the collection with the given name.

Find Documents
db.<collection_name>.find({ <key>: <value> })

It will return all the documents from the collection with the given name where the key matches the given value.

Find Documents
db.<collection_name>.find({ <key>: { $gt: <value> } })

It will return all the documents from the collection with the given name where the key is greater than the given value.

Find Documents
db.<collection_name>.find().count()

It will return the total number of documents in the collection with the given name.

Update Documents

Update Documents
db.<collection_name>.updateOne(
{ <key>: <value> },
{ $set: { <key>: <value> } }
)

It will update the first document from the collection with the given name where the key matches the given value.

Update Documents
db.<collection_name>.updateMany(
{ <key>: <value> },
{ $set: { <key>: <value> } }
)

It will update all the documents from the collection with the given name where the key matches the given value.

Delete Documents

Delete Documents
db.<collection_name>.deleteOne({ <key>: <value> })

It will delete the first document from the collection with the given name where the key matches the given value.

Delete Documents
db.<collection_name>.deleteMany({ <key>: <value> })

It will delete all the documents from the collection with the given name where the key matches the given value.

Drop Index

Drop Index
db.<collection_name>.dropIndex({ <key>: <value> })

It will drop the index from the collection with the given name where the key matches the given value.

Aggregate

Aggregate
db.<collection_name>.aggregate([
{ $match: { <key>: <value> } },
{ $group: { _id: <key>, total: { $sum: <value> } } }
])

It will return the total sum of the given value from the collection with the given name where the key matches the given value.

Conclusion

In this article, we have learned about MongoDB Shell Commands. I hope you have enjoyed this article.