MongoDB Overview
MongoDB is a document-oriented database built using C++ that provides scalable, high-performance storage for web applications. It represents data in BSON format (binary JSON), which allows for flexible data structures and supports complex data types.
Unlike traditional relational databases, MongoDB offers a flexible schema approach while providing a powerful query language similar to relational database SQL operations.
Key Features
- Document-based storage: Data is stored as documents in BSON format, allowing flexible schemas
- Indexing support: Create indexes on any field for faster query performance
- Horizontal scaling: Supports sharding to distribute data across multiple nodes
- Rich query language: JSON-style query expressions support embedded objects and arrays
- MapReduce: Built-in support for batch processing and data aggregation
- GridFS: Native solution for storing large files
- Server-side scripting: Execute JavaScript functions directly on the server
- Multi-language support: Compatible with Ruby, Python, Java, C++, PHP, C#, and more
Installation Steps
MongoDB runs on both Windows and Linux systems. The general installation process includes:
- Downloading the appropriate installasion package
- Extracting files to a custom directory
- Creating a data directory for database storage
- Configuring environment variables (optional but recommended)
- Starting the database server
Windows Installation
Step 1: Download
Obtain the Windows installer from the official MongoDB download center. Choose the version compatible with your Windows edition (Windows Server 2008 R2 64-bit and later recommended).
Step 2: Installation
Run the installer with default settings, or select "Custom" to specify your installation directory.
Create a data directory for MongoDB to store data base files. This directory must be created manually after installation. Place it at the root of a drive:
mkdir D:\mongodb_data\db
Step 3: Environment Variables
Add the MongoDB bin directory to your system PATH:
- Right-click My Computer → Properties → Advenced System Settings → Environment Variables
- Edit the Path variable in System variables
- Add the bin path (e.g.,
D:\MongoDB\bin)
Step 4: Starting MongoDB
Start the database server using the mongod command:
mongod --dbpath D:\mongodb_data\db
Upon successful startup, you should see:
waiting for connections on port 27017
Verify the server is running by accessing http://localhost:27017/ in your browser. The response should indicate MongoDB is running on the native driver port.
Linux Installation
Step 1: Download
Download the Linux tarball from the official MongoDB website:
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.4.tgz
Step 2: Extract and Setup
Extract the downloaded archive and move it to your preferred location:
tar -zxvf mongodb-linux-x86_64-3.4.4.tgz
mv mongodb-linux-x86_64-3.4.4 ~/mongodb
Create the data directory at the root level (default location MongoDB expects):
sudo mkdir -p /data/db
Step 3: Environment Configuration
Add MongoDB binaries to your PATH by editing ~/.bashrc:
export PATH=~/mongodb/bin:$PATH
Apply the changes:
source ~/.bashrc
Step 4: Starting MongoDB
Launch the MongoDB server:
./mongod
The server will use /data/db as the default data directory. To specify a custom path:
./mongod --dbpath /custom/path/to/data
Successful startup displays:
waiting for connections on port 27017
Connecting to MongoDB
Open a new terminal and launch the MongoDB shell:
./mongo
Test basic operations:
> 2 + 4
6
> db.users.insert({"name": "john", "country": "USA"})
WriteResult({ "nInserted" : 1 })
> db.users.insert({"name": "alice", "age": 25})
WriteResult({ "nInserted" : 1 })
> db.users.find()
{ "_id" : ObjectId("5912821b01ac84a44d1b59d4"), "name" : "john", "country" : "USA" }
{ "_id" : ObjectId("591270a7c9d90290d6e4dd41"), "name" : "alice", "age" : 25 }
Remote connections work similarly—access the server by navigating to http://<server-ip>:27017/ from any machine.