Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing and Configuring MongoDB on Windows

Tech 1

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:

  • --config points to the configuration file.
  • --install registers the service.
  • --serviceName assigns 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.