Open In App

Installing MongoDB on Windows with Python

Last Updated : 30 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Step 1: Download MongoDB Community Edition

Go to the official MongoDB website, Click here.

Choose the Windows installer (MSI package) and download it.

Step 2: Install MongoDB

  • Run the downloaded .msi installer.
  • Choose Complete setup during installation.
  • MongoDB gets installed typically at:

C:\Program Files\MongoDB\Server\<version>\

Step 3: Create MongoDB Data Directory

MongoDB needs a folder to store its data.

Open Command Prompt and run:

md C:\data\db

Step 4: Start MongoDB Server

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.

terminal_mongodb
Snapshot of terminal showing Mongodb runs on port 27027

Step 5: Add MongoDB to System PATH

To access mongo and mongod globally:

Start and search Environment Variables.

en_v
Environment Variables

Now, 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 MongoDB

Open a new Command Prompt (Admin mode). Start the MongoDB shell:

mongo

This opens an interactive shell to run MongoDB commands.

Note: 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).

Step 7 (Optional): Configure as a Windows Service

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

Note: Run these commands in Command Prompt (Admin mode).

Step 8: Install PyMongo in CMD

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:


Next Article
Practice Tags :

Similar Reads