MongoDB Shell mongosh Practice Questions with Solutions

Introduction

MongoDB Shell, commonly called mongosh, is the command-line interface used to interact directly with MongoDB. It allows you to connect to a MongoDB server, switch databases, create collections, insert documents, search data, and manage databases using commands. In this chapter, you will practice the most useful mongosh commands step by step so you can become comfortable working with MongoDB from the terminal. MongoDB Shell mongosh Practice Questions with Solutions to help you understand the concepts.

Q1. Start MongoDB Shell using mongosh

Problem Statement

Open the MongoDB Shell and connect to the MongoDB server running on your local computer.

MongoDB Command / Query

mongosh

Expected Output

Current Mongosh Log ID: 123456789
Connecting to: mongodb://127.0.0.1:27017/
Using MongoDB: 8.x.x
Using Mongosh: 2.x.x

test>

Explanation

The mongosh command starts the MongoDB Shell and normally connects to a MongoDB server running on the default local address:

mongodb://127.0.0.1:27017

The prompt such as:

test>

means you are now inside mongosh.


Q2. Check the current database

Problem Statement

After entering mongosh, find out which database you are currently using.

MongoDB Command / Query

db

Expected Output

test

Explanation

The db command displays the name of the database currently selected in mongosh.


Q3. Display the current database using getName()

Problem Statement

Use a MongoDB Shell method to display the name of the current database.

MongoDB Command / Query

db.getName()

Expected Output

test

Explanation

db.getName() returns the name of the database represented by the current db object.

This is useful when you want to explicitly check the database name in a script or while working with several databases.


Q4. Display all available databases

Problem Statement

Use mongosh to display the databases that your current MongoDB user can access.

MongoDB Command / Query

show dbs

Expected Output

admin      40.00 KiB
config     72.00 KiB
local      72.00 KiB
school     40.00 KiB

Explanation

show dbs is a mongosh shell helper that lists databases available to your current user.

The exact databases and sizes will be different on your computer.

A newly selected database may not appear in show dbs until it contains stored data.


Q5. Switch to another database

Problem Statement

Switch from the current database to a database named mongosh_practice.

MongoDB Command / Query

use mongosh_practice

Expected Output

switched to db mongosh_practice

Explanation

The use command changes the database that your db variable refers to.

Now check it:

db

Expected:

mongosh_practice

The database may not physically appear in show dbs until you store some data in it.


Q6. Create a collection and insert a document

Problem Statement

Inside the mongosh_practice database, create a students collection and insert one student document.

MongoDB Command / Query

db.students.insertOne({
    name: "Rahul",
    age: 15,
    course: "Python"
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

MongoDB can create the students collection automatically when the first document is inserted.

The document contains:

name   → Rahul
age    → 15
course → Python

insertedId is the unique _id generated for the document.


Q7. Display collections in the current database

Problem Statement

After creating the students collection, use mongosh to display the collections available in the current database.

MongoDB Command / Query

show collections

Expected Output

students

Explanation

show collections is a mongosh helper that displays collections in the currently selected database.

Since Question 6 created the students collection, it should now appear here.


Q8. Find documents from a collection

Problem Statement

Display all documents stored inside the students collection.

MongoDB Command / Query

db.students.find()

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rahul',
    age: 15,
    course: 'Python'
  }
]

Explanation

The find() method retrieves documents from a collection.

Here:

db.students.find()

means:

  • db → current database
  • students → collection
  • find() → retrieve documents

Q9. Find a student using a filter

Problem Statement

Find only the student whose course is "Python".

MongoDB Command / Query

db.students.find({
    course: "Python"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rahul',
    age: 15,
    course: 'Python'
  }
]

Explanation

The object inside find() is called a query filter.

{
    course: "Python"
}

MongoDB searches for documents where the course field has the value "Python".


Q10. Exit MongoDB Shell

Problem Statement

After completing your MongoDB work, close the mongosh session.

MongoDB Command / Query

exit

Expected Output

Bye

You can also use:

quit()

Explanation

Both exit and quit() can be used to leave the MongoDB Shell.

After exiting, you will return to your normal operating-system terminal.


Key Takeaways

  • mongosh is the command-line shell for working with MongoDB.
  • mongosh connects to a MongoDB server.
  • db shows the current database.
  • db.getName() returns the current database name.
  • show dbs displays accessible databases.
  • use databaseName switches databases.
  • show collections displays collections in the current database.
  • insertOne() inserts a document.
  • find() retrieves documents.
  • Query filters allow you to search for specific documents.
  • exit or quit() closes the MongoDB Shell.

FAQs

1. What is MongoDB Shell (mongosh)?

MongoDB Shell, or mongosh, is the command-line interface used to connect to MongoDB and execute MongoDB commands and queries.

2. How do I start mongosh?

Open your terminal or command prompt and run:

mongosh

MongoDB Server should be running for a normal local connection.

3. What is the difference between mongosh and MongoDB Compass?

mongosh is a command-line interface where you work using commands, while MongoDB Compass is a graphical user interface (GUI) for working with MongoDB.

4. What does the db command do in mongosh?

The db command displays the name of the database currently selected in the shell.

5. How do I switch databases in mongosh?

Use the use command:

use school

This switches the current database to school.

6. How do I see collections in mongosh?

Use:

show collections

It displays the collections available in the current database.

7. How do I exit mongosh?

You can use either:

exit

or:

quit()

Both close the current MongoDB Shell session.

SEO Package

SEO Title

MongoDB Shell (mongosh) Practice Questions with Solutions

Meta Description

MongoDB Shell (mongosh) Practice Questions with Solutions to learn mongosh commands, database switching, collections, queries, and shell basics step by step.

SEO Slug

mongodb-shell-mongosh-practice-questions-with-solutions

Focus Keywords

  1. MongoDB Shell mongosh Practice Questions
  2. MongoDB Shell Practice Questions with Solutions
  3. mongosh Practice Questions
  4. MongoDB mongosh
  5. MongoDB Shell Commands
  6. mongosh Commands for Beginners
  7. MongoDB Command Line
  8. Learn MongoDB Shell

Relevant Tags

MongoDB, MongoDB Shell, mongosh, MongoDB Practice Questions, MongoDB Practice Questions with Solutions, MongoDB Commands, mongosh Commands, MongoDB Tutorial, MongoDB for Beginners, MongoDB Database, MongoDB Collections, MongoDB Queries, MongoDB Command Line, MongoDB CLI, NoSQL Database, MongoDB Learning, Database Practice Questions, MongoDB Programming, MongoDB Development, MongoDB Exercises

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top