3. Core MongoDB Concepts: Databases, Collections, and Documents 📂

Intermediate

MongoDB structures data in a hierarchical format:

  • Database: The top-level container that holds collections.
  • Collection: Equivalent to a table in relational databases; a group of BSON documents.
  • Document: The basic unit of data, a JSON-like structure with key-value pairs.

Example:

// A user document
{
  "_id": ObjectId("507f1f77bcf86cd799439011"),
  "name": "Jane Doe",
  "age": 29,
  "email": "jane@example.com",
  "interests": ["reading", "hiking"]
}

Collections can store different types of documents, providing schema flexibility. This model simplifies the evolution of data schemas and supports nested objects and arrays seamlessly.

Understanding this structure is fundamental to designing efficient MongoDB schemas and querying data effectively.