Installing and Configuring MongoDB on Windows
Obtaining the Installation Package
Head to the MongoDB Community download page and choose the MSI installer for Windows.
Running the Installer
Launch the downloaded MSI and follow these steps.
- When the setup type selection appears, pick the Custom option so you can specify the installation path—for example,
D:\MongoDB. - Uncheck the option to install MongoDB Compass (the graphical client) during setup. Leaving it checked can cause the installation to hang for a long time. If you need Compass, download it separately from the MongoDB website.
After completing the wizard, the installation folder will contain the binaries and supporting files.
Post-installation Configuration
Launching MongoDB Manually
Open a terminal inside D:\MongoDB\bin and execute:
mongod --dbpath "D:\MongoDB\data"
D:\MongoDB\data is the directory where MongoDB stores its files. You should create this folder beforehand. Once the process starts, you can confirm its listening by visiting http://localhost:27017 in a browser.
Setting Up MongoDB as a Windows Service
Create a text file named mongod.cfg in the installation root (D:\MongoDB) with the following content:
# Storage location for data files
dbpath=D:\MongoDB\data
# Path to log output
logpath=D:\MongoDB\log\mongo.log
# Append to the log file instead of overwriting
logappend=true
# Enable write-ahead journaling
journal=true
# Suppress verbose diagnostic output
quiet=true
# TCP port for client connections
port=27017
Make sure the D:\MongoDB\log directory exists. Now open an elevated (Administrator) command prompt and navigate to D:\MongoDB\bin. Run:
mongod.exe --config "D:\MongoDB\mongod.cfg" --install --serviceName "MongoDB"
The parameters do the following:
--configpoints to the configuration file.--installregisters the service.--serviceNameassigns a display name to the Windows service.
Start the service with:
net start MongoDB
If the service fails to start because another mongod instance is already using the port, terminate the manual process that was launched earlier and try again.
Other useful commands:
net stop MongoDB
sc delete MongoDB
Alternatively, to uninstall the service manually:
D:\MongoDB\bin\mongod.exe --remove
Quick Verification
Connecting to the Shell
Type mongo in a command prompt. If the system cannot find the command, either navigate to D:\MongoDB\bin first or add that directory to your PATH environment variable. Once connected, the interactive MongoDB shell appears.
Basic Operations
Inside the shell, try simple arithmetic:
2 + 3
To see the current database:
db
Insert a document and retrieve it:
db.sample.insert({ value: 42 })
db.sample.find()
This stores a document with a field value equal to 42 in the sample collection and then fetches all documents from that collection.