Last Updated : 30 Jun, 2025
This guide will help you install and set up MongoDB on a Windows machine, along with Python integration using PyMongo. For a beginner-friendly experience, it's recommended to use an IDE like Spyder from the Anaconda distribution.
Go to the official MongoDB website, Click here.
Choose the Windows installer (MSI package) and download it.
Step 2: Install MongoDBStep 3: Create MongoDB Data DirectoryC:\Program Files\MongoDB\Server\<version>\
MongoDB needs a folder to store its data.
Open Command Prompt and run:
Step 4: Start MongoDB Servermd C:\data\db
Start the MongoDB server using this command:
mongod --dbpath "C:\data"
Alternative (if installed as a service):
net start MongoDB
Wait until you see a message indicating it's listening on port 27017.
Snapshot of terminal showing Mongodb runs on port 27027 Step 5: Add MongoDB to System PATHTo access mongo and mongod globally:
Start and search Environment Variables.
Environment VariablesNow, open Environment Variable under the System Variables and edit the Path. Add:
C:\Program Files\MongoDB\Server\<version>\bin
Like This:
Click OK and restart the terminal.
Step 6: Connect to MongoDBOpen a new Command Prompt (Admin mode). Start the MongoDB shell:
mongo
This opens an interactive shell to run MongoDB commands.
Step 7 (Optional): Configure as a Windows ServiceNote: When started with mongod, MongoDB runs only while the Command Prompt stays open. To keep it running in the background, install it as a service (see Step 7).
To run MongoDB in the background as a service:
1. Create necessary folders:
mkdir C:\data\db
mkdir C:\data\log
C:\data\db: Used to store all MongoDB data (required).
C:\data\log: Used to store MongoDB server logs (optional but needed when using a configuration file).
2. Create a config file at:
C:\Program Files\MongoDB\Server\<version>\mongod.cfg
Content:
systemLog:
destination: file
path: C:\data\log\mongod.log
storage:
dbPath: C:\data\db
3. Install MongoDB as a service:
"C:\Program Files\MongoDB\Server\<version>\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\<version>\mongod.cfg" --install
To save mongod.cfg in the MongoDB directory, open Notepad as administrator (search for it, then press Ctrl + Shift + Enter). This gives permission to save the file in system folders like C:\Program Files.
4. To start the service:
net start MongoDB
To stop:
net stop MongoDB
Step 8: Install PyMongo in CMDNote: Run these commands in Command Prompt (Admin mode).
Open Command Prompt or Anaconda Prompt as shown:
And run:
pip install pymongo
If using Conda:
conda install -c anaconda pymongo
You are now ready to use MongoDB with Python! You can start building databases, inserting data, and running queries using PyMongo. MongoDB will remain active as long as the server (or service) is running.
Related Articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4