MongoDB Installation and Setup Practice Questions with Solutions

Introduction

Before writing MongoDB queries, you need a working MongoDB environment. In this chapter, you will practice the basic steps required to install and verify MongoDB, start the MongoDB server, open mongosh, connect to a local server, check the MongoDB version, and work with a database. These examples are beginner-friendly and focus on practical setup tasks you can perform on your own computer. MongoDB Installation and Setup practice questions with solutions to help you understand the concepts.

1. Check the MongoDB Shell Version

Problem Statement

You want to check whether MongoDB Shell (mongosh) is installed on your computer.

MongoDB Command / Query

mongosh --version

Expected Output

2.x.x

The exact version depends on the version installed on your computer.

Explanation

The mongosh --version command displays the installed MongoDB Shell version.

If the command returns a version number, mongosh is installed and available from your terminal.


2. Check the MongoDB Server Version

Problem Statement

You want to check the version of the MongoDB server installed on your computer.

MongoDB Command / Query

mongod --version

Expected Output

db version v8.x.x
Build Info: {
    ...
}

The exact version and build information can be different.

Explanation

mongod is the MongoDB server process.

The command:

mongod --version

displays information about the installed MongoDB server, including its version.

Remember:

  • mongod → MongoDB server
  • mongosh → MongoDB Shell

3. Start MongoDB Server Manually

Problem Statement

Start the MongoDB server manually from the command line.

MongoDB Command / Query

mongod

Expected Output

{"t":{"$date":"..."},"s":"I","c":"NETWORK","msg":"Waiting for connections","attr":{"port":27017}}

Explanation

The mongod command starts the MongoDB database server.

By default, MongoDB commonly listens for local connections on port:

27017

When you see a message indicating that MongoDB is waiting for connections, the server is ready to accept connections.

Keep this terminal window running while using the server.


4. Connect to the Local MongoDB Server

Problem Statement

The MongoDB server is running locally. Connect to it using MongoDB Shell.

MongoDB Command / Query

mongosh

Expected Output

Connecting to: mongodb://127.0.0.1:27017/
...
test>

Explanation

mongosh starts the MongoDB Shell and connects to the local MongoDB server.

The address:

127.0.0.1:27017

means that the connection is being made to MongoDB running on your own computer.

Once you see:

test>

you can start executing MongoDB commands.


5. Connect Using a MongoDB Connection String

Problem Statement

Connect to a MongoDB server running on the default local port using an explicit connection string.

MongoDB Command / Query

mongosh "mongodb://127.0.0.1:27017"

Expected Output

Connecting to: mongodb://127.0.0.1:27017/
...
test>

Explanation

MongoDB uses a connection string to describe where the database server is located.

Here:

mongodb://127.0.0.1:27017

contains:

  • mongodb:// → MongoDB connection protocol
  • 127.0.0.1 → your local computer
  • 27017 → MongoDB’s default port

Using an explicit connection string becomes especially useful when working with different MongoDB servers.


6. Check the Current Database

Problem Statement

After connecting to MongoDB, find out which database is currently selected.

MongoDB Command / Query

db

Expected Output

test

Explanation

The db command shows the current database.

When you connect to MongoDB without selecting another database, the shell commonly starts with:

test

You can switch to another database later using the use command.


7. Switch to a Database for Practice

Problem Statement

Create a working database named mongodb_practice for your MongoDB exercises.

MongoDB Command / Query

use mongodb_practice

Expected Output

switched to db mongodb_practice

Explanation

The use command switches the current database.

Now check the selected database:

db

Expected Output

mongodb_practice

One important point for beginners: selecting a database with use does not necessarily create a physical database immediately. MongoDB creates the database when you actually store data in it.


8. Create Your First Collection

Problem Statement

Inside the mongodb_practice database, create a collection called students.

MongoDB Command / Query

db.createCollection("students")

Expected Output

{ ok: 1 }

Explanation

A collection stores MongoDB documents.

The command:

db.createCollection("students")

creates a collection named students in the currently selected database.

You can verify it with:

show collections

Expected Output

students

9. Insert a Test Document to Verify the Setup

Problem Statement

Insert one student document into the students collection to confirm that your MongoDB installation is working correctly.

MongoDB Command / Query

db.students.insertOne({
    name: "Aman",
    age: 14,
    course: "MongoDB"
})

Expected Output

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

Explanation

insertOne() adds one document to the collection.

The document contains:

{
    name: "Aman",
    age: 14,
    course: "MongoDB"
}

If MongoDB returns:

acknowledged: true

the insertion was successfully accepted by the database.

MongoDB automatically generates an _id for the document when you do not provide one.


10. Verify the Complete MongoDB Setup

Problem Statement

You have installed MongoDB, started the server, created a database, created a collection, and inserted a document. Now verify that the document can be retrieved.

MongoDB Command / Query

db.students.find()

Expected Output

[
    {
        _id: ObjectId('...'),
        name: 'Aman',
        age: 14,
        course: 'MongoDB'
    }
]

Explanation

The find() method retrieves documents from the collection.

If you can see the inserted student document, your basic MongoDB setup is working correctly.

The complete practice flow was:

MongoDB Server
      ↓
mongosh
      ↓
mongodb_practice database
      ↓
students collection
      ↓
Student document

This is the basic environment you will use for the upcoming MongoDB practice chapters.

Key Takeaways

  • mongod is the MongoDB server process.
  • mongosh is the MongoDB Shell.
  • mongod --version checks the MongoDB server version.
  • mongosh --version checks the MongoDB Shell version.
  • MongoDB commonly uses port 27017 for local connections.
  • mongosh connects to the local MongoDB server.
  • db displays the current database.
  • use databaseName switches to a database.
  • db.createCollection() creates a collection.
  • insertOne() inserts a document.
  • find() retrieves documents.
  • A successful acknowledged: true response indicates that an operation was accepted successfully.

FAQs

1. How do I install MongoDB on my computer?

Download and install MongoDB Community Server for your operating system, then install MongoDB Shell (mongosh) if it is not included with your installation.

2. What is the difference between mongod and mongosh?

mongod runs the MongoDB database server, while mongosh is the command-line shell used to connect to and work with that server.

3. What is the default MongoDB port?

The standard MongoDB server port is 27017.

4. How can I check whether MongoDB is installed?

You can check the MongoDB server with:

mongod --version

and the MongoDB Shell with:

mongosh --version

5. Why does mongosh fail to connect to MongoDB?

A common reason is that the MongoDB server is not running. Start the MongoDB server first and then try connecting with mongosh.

6. Does use mongodb_practice immediately create a MongoDB database?

Not necessarily. The use command selects the database. MongoDB generally creates the database when data is actually stored in it.

7. How can I verify that my MongoDB installation is working?

Connect with mongosh, select a database, create or use a collection, insert a document, and retrieve it with find(). If these operations work successfully, your basic MongoDB setup is working.

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

Scroll to Top