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.
To work with MongoDB, you should understand these tools:
MongoDB Compass is optional. You can use the shell to perform database operations without it.
Follow these steps:
Use the official MongoDB website to obtain the installer and review the current system requirements.
After downloading the installer, follow these steps:
.msi installer.
MongoDB Shell, also called mongosh, is a
command-line tool used to connect to MongoDB and execute
commands.
mongosh executable is available in your PATH.MongoDB Shell is a separate tool from the database server in many installation setups.
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.
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
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.
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.
Possible solution: Check whether MongoDB Shell is installed and add its executable directory to the system PATH if necessary. Open a new terminal afterward.
Possible solution: Confirm that the MongoDB server is running and listening on the expected host and port.
Possible solution: Create the directory
specified by --dbpath and ensure the MongoDB
process has permission to access it.
Possible solution: Check whether the server was installed as a Windows service. If it was not, follow the manual startup instructions for your installation.
mongosh --version.practice_db.students.find().Q1. Which executable runs the MongoDB database server?
Q2. Which command checks the MongoDB Shell version?
Q3. What is the default MongoDB server port?