Skip to content

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

  1. Document-Oriented: Stores data in BSON (Binary JSON) format.
  2. Schema-less: No predefined schema, flexible structure.
  3. High Availability: Uses replica sets for redundancy and failover.
  4. Horizontal Scalability: Supports sharding across servers.
  5. Indexing: Supports various types (single, compound, geospatial, etc.).
  6. Aggregation Framework: For complex data operations.
  7. Flexible Query Language: Supports rich and expressive queries.
  8. Ad Hoc Queries: Dynamic query capability without a schema.
  9. Document Validation: Enforces rules before data is inserted or updated.
  10. 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 info
  • config — Stores MongoDB server configuration
  • local — 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

  1. Download MongoDB from the MongoDB Download Center.
  2. Select:
    • Version: 7.0.4
    • OS: Windows x64
    • Package: msi
  3. Follow the installer steps:
    • Accept license
    • Choose Complete setup
    • Select Run as Network Service
    • Finish installation
  4. Add MongoDB’s bin path to the Environment Variables.
  5. Create C:\data\db folder manually.
  6. Run the MongoDB server using:

How to Install MongoDB on macOS

  1. Install Homebrew (if not installed):

    Terminal window
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install MongoDB:

    Terminal window
    # Add the MongoDB repository
    brew tap mongodb/brew
    # Install MongoDB Community Edition
    brew install mongodb-community
  3. Manage MongoDB Service:

    Terminal window
    # Start the MongoDB service
    brew services start mongodb-community
  4. Stop the MongoDB service:

    Terminal window
    # Stop the MongoDB service
    brew services stop mongodb-community
  5. Restart the MongoDB service:

    Terminal window
    # Restart the MongoDB service
    brew services restart mongodb-community
  6. View MongoDB service status

    Terminal window
    # View MongoDB service status
    brew 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.


Pro tip: Keep your MongoDB service healthy and it will keep your data safe! 🔒