Installing MongoDB on Windows
In this tutorial, we’ll walk through how to install MongoDB Community Edition on Windows. By the end, you’ll be able to run MongoDB locally on your system using both the shell and GUI.
Step 1: Download MongoDB Installer
Visit the official MongoDB download page:
https://www.mongodb.com/try/download/community
Select the following options:
- Platform: Windows
- Package: MSI
- Version: Choose the latest stable release
Click Download and save the MSI file to your system.
Step 2: Run the Installer
Double-click the downloaded .msi
file to start the installation wizard. Follow these steps:
- Accept the License Agreement
- Choose Complete Setup
- Enable Install MongoDB as a Service (recommended)
- Select Run MongoDB as Network Service User
- Finish installation
Step 3: Verify Installation
After installation, MongoDB should be available in your system path.
Open Command Prompt and run:
mongod --version
Output:
db version v7.0.0 Build Info: ...
This confirms MongoDB is installed. Now, let’s start the database server.
Step 4: Create MongoDB Data Directory
MongoDB needs a folder to store its data files. By default, it looks for C:\data\db
.
Run the following commands to create the required folder:
md \data\db
Make sure the folder exists or MongoDB will not start.
Step 5: Start MongoDB Server
Now start the MongoDB server using this command:
mongod
Output:
{"t":{"$date":"..."},"s":"I",...,"msg":"Waiting for connections","attr":{"port":27017,...}}
This means MongoDB is running and waiting for client connections on port 27017.
Intuition Check
Q: Why does MongoDB require a data directory like C:\data\db
?
A: MongoDB stores all of its database files (collections, indexes, documents) in this directory. Without it, MongoDB has nowhere to persist data.
Step 6: Open Another Command Prompt and Connect to MongoDB
Keep the server window open. Open another terminal window and run:
mongo
Output:
MongoDB shell version v7.0.0 connecting to: mongodb://127.0.0.1:27017/ test>
Now you're inside the MongoDB shell and can start issuing database commands.
Step 7: Insert and Read a Document
Let’s insert a document into a new collection and read it back:
use mydb
db.users.insertOne({ name: "Alice", age: 25 })
db.users.find()
Output:
{ _id: ObjectId("..."), name: "Alice", age: 25 }
Explanation:
use mydb
switches to a new database calledmydb
.insertOne()
adds a new document to theusers
collection.find()
retrieves documents from the collection.
Step 8: Install MongoDB Compass (Optional GUI)
If you prefer a visual interface, install MongoDB Compass during the installation wizard or from:
https://www.mongodb.com/try/download/compass
Compass allows you to:
- View databases and collections
- Run queries without using the shell
- Visualize schema and indexes
Summary
In this tutorial, you installed MongoDB on Windows, started the database service, ran your first shell commands, and even inserted data. You’re now ready to start working with collections and documents.
Up Next:
In the next lesson, we’ll explore MongoDB’s structure — databases, collections, and documents — and learn how MongoDB organizes data.