MongoDB - Getting StartedMongoDB - Getting Started1

Understanding Databases, Collections, and Documents in MongoDB

Understanding Databases, Collections, and Documents in MongoDB

MongoDB organizes data in a structure that is different from traditional relational databases. To use MongoDB effectively, it's essential to understand its three core building blocks: databases, collections, and documents.

What is a Database in MongoDB?

A database in MongoDB is a container that holds collections, which in turn contain documents. Each database has its own set of files on the disk and is isolated from other databases.

What is a Collection?

A collection is a group of MongoDB documents, similar to a table in relational databases. However, collections are schema-less, meaning documents inside them can have different fields.

What is a Document?

A document is a single record in MongoDB, stored in BSON (Binary JSON) format. It is similar to a row in an SQL table, but far more flexible — allowing nested objects and arrays.

Example: Creating a Database and Collection

Let’s create a new database and collection using the MongoDB Shell.

    mongo

    use school
    db.createCollection("students")
    { "ok" : 1 }

Explanation: Here, we switch to a new database called school. If it doesn't exist, MongoDB will create it once we insert data. Then we create a collection called students.

Inserting a Document into the Collection

Now let’s insert a student document into the students collection.


    db.students.insertOne({
      name: "Rahul Sharma",
      age: 20,
      courses: ["Math", "Physics"],
      address: {
        city: "Delhi",
        pincode: 110001
      }
    })
    {
      acknowledged: true,
      insertedId: ObjectId("...")
    }

Explanation: This document represents one student. It includes a name, age, an array of courses, and a nested object for address. This flexible schema allows us to store rich, structured data in one place without complex joins.

Question:

Q: Can the next student we insert have a completely different structure?

A: Yes! MongoDB collections do not require all documents to follow the same schema.

Example: Inserting a Different-Shaped Document


    db.students.insertOne({
      name: "Aditi Mehta",
      year: "Final",
      hobbies: ["painting", "reading"]
    })
    {
      acknowledged: true,
      insertedId: ObjectId("...")
    }

Explanation: This document doesn't have age, courses, or address fields. That’s perfectly fine in MongoDB.

Viewing All Documents

You can view the inserted documents using find():


    db.students.find().pretty()
    {
      "_id": ObjectId("..."),
      "name": "Rahul Sharma",
      "age": 20,
      "courses": ["Math", "Physics"],
      "address": {
        "city": "Delhi",
        "pincode": 110001
      }
    }
    {
      "_id": ObjectId("..."),
      "name": "Aditi Mehta",
      "year": "Final",
      "hobbies": ["painting", "reading"]
    }

Question:

Q: Do we need to define the fields before inserting documents?

A: No. MongoDB allows dynamic documents, so you can insert any structure at any time. This is great for evolving data models.

Summary

  • Database – Top-level container for collections
  • Collection – A set of documents (like a table)
  • Document – Individual data entry (like a row), stored in BSON

MongoDB's structure is highly flexible and intuitive once you grasp these three concepts. In the next topic, we’ll explore MongoDB’s supported data types in more detail.