Lesson 3 of 50 – Installing MongoDB
6%

Installing MongoDB

In this lesson, you will learn how to install MongoDB on Windows, install MongoDB Shell, connect to your MongoDB server, and run your first database commands.

1. What Do We Need to Install MongoDB?

To work with MongoDB, you should understand these tools:

  • MongoDB Community Server: Runs the MongoDB database server on your computer.
  • MongoDB Shell (mongosh): Lets you execute MongoDB commands.
  • MongoDB Compass: A graphical interface for viewing and managing databases.

MongoDB Compass is optional. You can use the shell to perform database operations without it.

Important: Installing MongoDB Server, MongoDB Shell, and MongoDB Compass may involve separate downloads or installers.

2. Download MongoDB Community Server

Follow these steps:

  1. Open your web browser.
  2. Visit the official MongoDB Community Server download page.
  3. Select Windows as your operating system.
  4. Select the appropriate installer package.
  5. Choose the version and architecture supported by your computer.
  6. Click Download.
Download MongoDB Community Server

Use the official MongoDB website to obtain the installer and review the current system requirements.

3. Install MongoDB on Windows

After downloading the installer, follow these steps:

  1. Open the downloaded .msi installer.
  2. Accept the license agreement.
  3. Select the Complete installation option if appropriate.
  4. Configure MongoDB as a Windows service if you want it to start and run as a background service.
  5. Choose the service settings and data directory.
  6. Complete the installation.
Note: Installer screens and options can differ by MongoDB version. Follow the options displayed by your installer.

4. Install MongoDB Shell (mongosh)

MongoDB Shell, also called mongosh, is a command-line tool used to connect to MongoDB and execute commands.

  1. Open the official MongoDB Shell download page.
  2. Select the Windows package suitable for your system.
  3. Download and install or extract the package as directed.
  4. Ensure the mongosh executable is available in your PATH.
Download MongoDB Shell

MongoDB Shell is a separate tool from the database server in many installation setups.

5. Verify the Installation

Open Command Prompt or PowerShell and execute:

mongosh --version

If MongoDB Shell is installed and available in your PATH, the command displays its version.

To check the database server version, try:

mongod --version

The mongod command is the MongoDB server executable. It may not be available in your PATH if the installation directory was not added to PATH.

6. Start the MongoDB Server

If you configured MongoDB as a Windows service, it may already be running.

You can check its status using an administrator Command Prompt:

net start MongoDB

If the service is already running, Windows may report that it has already been started.

To start the server manually instead, you need a valid data directory. For example, create a directory:

mkdir C:\data\db

Then run the server executable with that directory:

mongod --dbpath C:\data\db
Run the manual server command only when you are not already running another MongoDB server on the same port. Keep the terminal open while the server runs.

7. Connect to MongoDB

After the server is running, open a new terminal and enter:

mongosh

By default, MongoDB Shell attempts to connect to a local MongoDB server at localhost:27017.

You can also specify the connection string:

mongosh "mongodb://localhost:27017"

If the connection succeeds, you can start executing MongoDB commands.

8. Run Your First MongoDB Commands

Step 1: Display the server version

db.version()

Step 2: Display the current database

db

Step 3: Select a database

use school_db

Step 4: Insert a document

db.students.insertOne({
    name: "Rahul",
    age: 20,
    course: "ADCA"
})

Step 5: Display the documents

db.students.find()

MongoDB creates the database and collection when needed during data insertion. Selecting a database with use alone does not necessarily create it permanently.

9. Common Installation Problems

Problem 1: mongosh is not recognized

Possible solution: Check whether MongoDB Shell is installed and add its executable directory to the system PATH if necessary. Open a new terminal afterward.


Problem 2: Connection refused

Possible solution: Confirm that the MongoDB server is running and listening on the expected host and port.


Problem 3: Data directory does not exist

Possible solution: Create the directory specified by --dbpath and ensure the MongoDB process has permission to access it.


Problem 4: MongoDB service is not found

Possible solution: Check whether the server was installed as a Windows service. If it was not, follow the manual startup instructions for your installation.

10. Practice Assignment

  1. Download MongoDB Community Server from the official website.
  2. Install MongoDB on your Windows computer.
  3. Install MongoDB Shell if it is not already installed.
  4. Check the version using mongosh --version.
  5. Connect to your local MongoDB server.
  6. Create or select a database named practice_db.
  7. Insert one student document into a collection named students.
  8. Retrieve the document using find().

🧠 Quick Quiz

Q1. Which executable runs the MongoDB database server?




Q2. Which command checks the MongoDB Shell version?




Q3. What is the default MongoDB server port?