Lesson 1 of 50 – Introduction to MongoDB
2%

Introduction to MongoDB

MongoDB is a popular NoSQL database used to store and manage data in a flexible document format. It is commonly used in modern web applications, mobile applications, and backend systems.

1. What Is MongoDB?

MongoDB is an open-source, document-oriented database. Instead of storing data in traditional rows and columns, MongoDB stores data as documents inside collections.

For example, a student record can be stored as:

{
    "_id": 1,
    "name": "Rahul",
    "age": 20,
    "course": "ADCA",
    "city": "Aurangabad"
}

In this example, the student's information is stored in a single document using field-value pairs.

2. What Is a NoSQL Database?

NoSQL means "Not Only SQL." NoSQL databases provide different ways to store and manage data without requiring the traditional relational table structure.

Common types of NoSQL databases include:

  • Document databases: MongoDB
  • Key-value databases: Redis
  • Wide-column databases: Apache Cassandra
  • Graph databases: Neo4j

MongoDB is a document database because it stores records as documents.

3. Features of MongoDB

  • Document-based storage: Stores data in BSON documents.
  • Flexible schema: Documents in the same collection can have different fields.
  • Scalability: Supports horizontal scaling through sharding.
  • Indexing: Indexes can improve query performance.
  • Aggregation: Processes and summarizes data using aggregation pipelines.
  • Replication: Replica sets maintain multiple copies of data for availability.

4. SQL Database vs MongoDB

SQL Database MongoDB
Stores data in tables Stores data in collections
Uses rows Uses documents
Uses columns Uses fields
Commonly uses a predefined schema Supports flexible document structures
Example: MySQL Example: MongoDB

Both SQL and NoSQL databases can be useful. The right choice depends on the application's data structure, consistency requirements, queries, and scalability needs.

5. Database, Collection, and Document

MongoDB uses three important concepts:

  1. Database: Contains collections.
  2. Collection: Contains related documents.
  3. Document: Contains data stored as field-value pairs.

Example structure:

soopro_db
    |
    |-- students
            |
            |-- Document 1
            |-- Document 2
            |-- Document 3

Here, soopro_db is the database and students is a collection containing student documents.

6. Example of a MongoDB Document

Consider the following document for a student in a computer training institute:

{
    "_id": 101,
    "name": "Amit Kumar",
    "age": 21,
    "course": "Web Development",
    "feesPaid": 5000,
    "isActive": true
}

Explanation of the fields:

  • _id: Unique identifier for the document.
  • name: Student's name.
  • age: Student's age.
  • course: Course selected by the student.
  • feesPaid: Amount of fees paid.
  • isActive: Boolean value indicating status.

MongoDB normally generates an ObjectId for _id if you do not provide one when inserting a document.

7. Where Is MongoDB Used?

  • Student management systems
  • Library management systems
  • E-commerce applications
  • Social media applications
  • Content management systems
  • Real-time applications
  • Business dashboards and analytics

8. Basic MongoDB Example

The following commands can be executed in MongoDB Shell (mongosh).

Select or create a database:

use soopro_db

Insert a student document:

db.students.insertOne({
    name: "Rahul",
    age: 20,
    course: "ADCA"
})

Display student documents:

db.students.find()

The insertOne() method inserts one document. The find() method retrieves documents from the collection.

Note: These are MongoDB Shell commands, not PHP or JavaScript browser commands. Install and connect to MongoDB before running them.

9. Practice Assignment

Complete the following beginner assignments:

  1. Write the full form of NoSQL.
  2. Explain the difference between a collection and a document.
  3. Write a MongoDB document for an employee.
  4. Write a command to select a database named school_db.
  5. Write a command to insert a student into the students collection.

🧠 Quick Quiz

Q1. What type of database is MongoDB?




Q2. What contains documents in MongoDB?




Q3. Which method inserts one document?