Introduction to MongoDB
What is a Database?
A database is an organized collection of structured information or data, typically stored in a computer system and managed using a DBMS (Database Management System).
Hierarchy of MongoDB
- One database can contain multiple collections
- One collection is mandatory
- A collection contains multiple documents
- A document contains multiple fields
What is a Collection?
In MongoDB, a collection is a grouping of MongoDB documents. Each document is a set of key-value pairs and represents a unit of data within the collection.
What is a Document?
A document is the basic unit of data in MongoDB, stored as key-value pairs (like a JSON object). Documents can vary in structure and are stored inside collections.
What is MongoDB?
MongoDB is a document-oriented NoSQL database known for:
- High scalability, flexibility, and performance
- Storing data in JSON-like documents
- Being open-source and cross-platform
History of MongoDB
- 2007: Developed by 10gen
- 2009: Open-source release and commercial support
- 2013: 10gen renamed to MongoDB Inc.
- 2017: MongoDB went public (IPO)
- 2018: Version 4.0.4 released
- Latest: MongoDB 7.0 (includes changes from Rapid Releases 6.1–6.3)
Features of MongoDB
- Document-Oriented: Stores data in BSON (Binary JSON) format.
- Schema-less: No predefined schema, flexible structure.
- High Availability: Uses replica sets for redundancy and failover.
- Horizontal Scalability: Supports sharding across servers.
- Indexing: Supports various types (single, compound, geospatial, etc.).
- Aggregation Framework: For complex data operations.
- Flexible Query Language: Supports rich and expressive queries.
- Ad Hoc Queries: Dynamic query capability without a schema.
- Document Validation: Enforces rules before data is inserted or updated.
- Security: Built-in authentication, authorization, encryption, and auditing.
Advantages of MongoDB
- Supports complex queries (field, range-based, string matching)
- Allows indexing on any fields
- Uses JavaScript objects instead of stored procedures
- Dynamic schema
- Easy to scale
- Native support for sharding (data partitioning)
Reserved Databases in MongoDB
admin— Stores user credentials and authentication infoconfig— Stores MongoDB server configurationlocal— Stores local server-specific data
Supported Data Types in MongoDB
- Null
- Boolean
- Number
- String
- Date
- Regular expression
- Array
- Embedded document
- ObjectId
- Binary Data
- Code
How to Install MongoDB on Windows
- Download MongoDB from the MongoDB Download Center.
- Select:
- Version:
7.0.4 - OS:
Windows x64 - Package:
msi
- Version:
- Follow the installer steps:
- Accept license
- Choose Complete setup
- Select Run as Network Service
- Finish installation
- Add MongoDB’s
binpath to the Environment Variables. - Create
C:\data\dbfolder manually. - Run the MongoDB server using:
How to Install MongoDB on macOS
Method 1: Using Homebrew (Recommended)
-
Install Homebrew (if not installed):
Terminal window /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -
Install MongoDB:
Terminal window # Add the MongoDB repositorybrew tap mongodb/brew# Install MongoDB Community Editionbrew install mongodb-community -
Manage MongoDB Service:
Terminal window # Start the MongoDB servicebrew services start mongodb-community -
Stop the MongoDB service:
Terminal window # Stop the MongoDB servicebrew services stop mongodb-community -
Restart the MongoDB service:
Terminal window # Restart the MongoDB servicebrew services restart mongodb-community -
View MongoDB service status
Terminal window # View MongoDB service statusbrew services list | grep mongodb
Why developers love MongoDB?
Simple hai! MongoDB developers ko flexibility deta hai. Aap bina tension ke apna data structure change kar sakte hain, aur scale karna to aur bhi asan hai. Agar aapka application grow kar raha hai ya requirements change ho rahi hain, to MongoDB best friend sabit hota hai.