MongoDB - Getting StartedMongoDB - Getting Started1

What is MongoDB? (NoSQL vs SQL)

What is MongoDB? (NoSQL vs SQL)

MongoDB is a document-oriented NoSQL database used for high-volume data storage. Instead of using tables and rows like traditional relational databases (SQL), MongoDB uses collections and documents.

Each document in MongoDB is a JSON-like object called BSON (Binary JSON), which allows for embedded data and flexible schemas.

Why learn MongoDB?

MongoDB is widely used in modern applications due to its:

  • Flexible schema — no need to define tables up front
  • Scalability — great for big data and horizontal scaling
  • JSON-like structure — naturally maps to code objects (JavaScript, Python, etc.)

SQL vs NoSQL — The Difference

SQL (Relational)NoSQL (MongoDB)
Uses tables and rowsUses collections and documents
Fixed schemaDynamic schema (flexible)
Relationships using joinsEmbedded documents or references
Good for structured dataGreat for semi-structured or unstructured data

Intuition Check

Q: If you’re building an application where different users have different fields in their profile, would SQL or MongoDB be easier to work with?

A: MongoDB! Because you don’t need to predefine a schema. One document can have a name and age, another might have name, age, and hobbies — and that’s totally okay.

Example 1: SQL Table vs MongoDB Document

In SQL:


    CREATE TABLE users (
      id INT,
      name VARCHAR(50),
      age INT
    );

    INSERT INTO users VALUES (1, 'Alice', 25);

In MongoDB:


    db.users.insertOne({
      _id: 1,
      name: "Alice",
      age: 25
    });
    {
      acknowledged: true,
      insertedId: 1
    }

Explanation: MongoDB stores data as documents in collections. There's no need to define the structure before inserting data. It's simple and fast.

Example 2: Flexible Schema in Action

You can insert different shaped documents into the same collection:


    db.users.insertMany([
      { name: "Bob", age: 30 },
      { name: "Charlie", hobbies: ["chess", "cycling"], location: "India" }
    ]);
    {
      acknowledged: true,
      insertedIds: [ObjectId("..."), ObjectId("...")]
    }

Explanation: Notice how the second document has completely different fields? MongoDB allows that flexibility, which is powerful in agile development environments.

Intuition Check

Q: Is it possible to store nested objects or arrays in SQL tables directly?

A: Not easily. SQL would need foreign keys and joins. MongoDB can directly store nested arrays and objects. This is useful for storing things like a user's multiple addresses or product reviews inside a single document.

Example 3: Embedded Document


    db.products.insertOne({
      name: "Laptop",
      price: 70000,
      specs: {
        brand: "Dell",
        ram: "16GB",
        storage: "512GB SSD"
      }
    });
    {
      acknowledged: true,
      insertedId: ObjectId("...")
    }

Explanation: The specs field is a sub-document (nested object). MongoDB handles this natively without additional joins or relations.

Key Terms Recap

  • Document – A single record (like a row), stored in BSON format
  • Collection – A group of documents (like a table)
  • BSON – Binary JSON, supports more data types than JSON

Summary

MongoDB is a flexible, document-based NoSQL database perfect for modern applications with semi-structured data. It allows fast development, easy scaling, and natural data modeling using embedded documents and arrays.

Up Next:

Now that you know what MongoDB is and how it differs from SQL, let’s move on to installing MongoDB on your system and setting up MongoDB Compass.