How to Install and Use MongoDB on Windows
Step-by-step guide for beginners
Introduction
MongoDB is a popular NoSQL database used for storing JSON-like documents. If you’re a Windows user and want to start working with MongoDB, this guide is for you. We’ll walk through installation, setup, and basic usage.
Keywords:
MongoDB, Windows, NoSQL, MongoDB installation, MongoDB commands, MongoDB Compass, MongoDB shell, JSON database
Step 1: Download MongoDB for Windows
- Go to the official MongoDB download page: https://www.mongodb.com/try/download/community
- Choose the latest stable version.
- Select Windows as the operating system.
- Download the MSI installer.
Step 2: Install MongoDB
- Run the downloaded MSI installer.
- Choose Complete setup when prompted.
- Make sure to select the option: "Install MongoDB as a Service".
- Also install MongoDB Compass (MongoDB’s GUI) if you want.
Step 3: Verify Installation
Open Command Prompt and type:
mongod --version
If installed correctly, you will see the MongoDB server version.
Step 4: Start MongoDB Server
If you installed as a service, MongoDB should already be running. But you can manually start the MongoDB server like this:
"C:\Program Files\MongoDB\Server\7.0\bin\mongod.exe"
(Adjust the path if you installed a different version)
Step 5: Open MongoDB Shell (mongosh)
To start using MongoDB, open another Command Prompt and type:
mongosh
This opens the MongoDB Shell where you can run commands.
Step 6: Basic MongoDB Commands
Create or Switch Database:
use mydatabase
Create Collection and Insert Data:
db.users.insertOne({ name: "Shubham", age: 22 })
Find Data:
db.users.find()
Update Data:
db.users.updateOne({ name: "Shubham" }, { $set: { age: 23 } })
Delete Data:
db.users.deleteOne({ name: "Shubham" })
Step 7: Using MongoDB Compass (Optional)
MongoDB Compass is a graphical user interface (GUI) for MongoDB. It lets you visualize databases, collections, and documents easily.
- Open Compass from the Start Menu.
- For localhost connection, use:
mongodb://localhost:27017
- Click "Connect" to see your databases.
Step 8: Setting MongoDB Path (Optional)
If you want to run mongod
and mongosh
from any directory in Command Prompt, add MongoDB’s bin
folder to your system PATH.
Steps:
- Right-click on "This PC" → Properties → Advanced system settings → Environment Variables.
- Under "System Variables", find "Path" → Click Edit → Add:
C:\Program Files\MongoDB\Server\7.0\bin
Common Errors and Fixes
- Port already in use: Stop any other services running on port 27017.
- Command not recognized: Ensure you added MongoDB to your PATH.
- Cannot connect with Compass: Make sure MongoDB server is running.
Conclusion
Congratulations! You’ve successfully installed and run MongoDB on your Windows system. Now you’re ready to start building NoSQL applications with MongoDB.
Happy Coding!
0 Comments