Skip to main content

mongodb-basics

Basic​

In MongoDB, data is organized into databases, collections, and documents. These concepts are fundamental to understanding how MongoDB stores and manages data. Let’s break them down:


1. Databases​

  • A database is a container for collections.
  • It is the top-level organizational unit in MongoDB.
  • You can have multiple databases in a single MongoDB instance.
  • Each database is independent and can have its own set of collections and permissions.

Example:​

  • A MongoDB instance might have databases like:
    • school (for managing student and teacher data).
    • ecommerce (for managing products and orders).
    • blog (for managing posts and comments).

2. Collections​

  • A collection is a group of documents.
  • It is analogous to a table in relational databases (e.g., MySQL, PostgreSQL).
  • Collections are schema-less, meaning documents within a collection can have different structures.
  • Collections are stored within a database.

Example:​

  • In the school database, you might have collections like:
    • students (to store student records).
    • teachers (to store teacher records).
    • courses (to store course information).

3. Documents​

  • A document is a single record in a collection.
  • It is stored in BSON (Binary JSON) format, which is a binary representation of JSON-like data.
  • Documents are schema-less, meaning each document in a collection can have a different structure.
  • Documents consist of key-value pairs, where keys are strings and values can be of various data types (e.g., strings, numbers, arrays, objects, dates, etc.).

Example:​

  • A document in the students collection might look like this:
    {
    "_id": ObjectId("65a1b2c3d4e5f6a7b8c9d0e1"),
    "name": "John Doe",
    "age": 20,
    "email": "john.doe@example.com",
    "courses": ["Math", "Science", "History"]
    }

Key Points to Remember​

  1. Hierarchy:

    • Database → Collections → Documents.
    • Example: school (database) → students (collection) → { "name": "John Doe", ... } (document).
  2. Schema-less Design:

    • Unlike relational databases, MongoDB does not enforce a fixed schema for collections.
    • Documents in the same collection can have different structures.
  3. _id Field:

    • Every document in MongoDB must have a unique _id field, which acts as the primary key.
    • If you don’t provide an _id, MongoDB will automatically generate one using ObjectId.
  4. Flexibility:

    • MongoDB’s document model allows for nested data structures (e.g., arrays, sub-documents), making it highly flexible for storing complex data.

Example in Practice​

Let’s say you’re building a blogging platform. Here’s how you might organize your data in MongoDB:

Database: blog​

  • Collection: users

    • Documents:
      {
      "_id": ObjectId("65a1b2c3d4e5f6a7b8c9d0e1"),
      "username": "johndoe",
      "email": "john@example.com",
      "joinedAt": ISODate("2023-10-01T00:00:00Z")
      }
  • Collection: posts

    • Documents:
      {
      "_id": ObjectId("65a1b2c3d4e5f6a7b8c9d0e2"),
      "title": "Introduction to MongoDB",
      "content": "MongoDB is a NoSQL database...",
      "authorId": ObjectId("65a1b2c3d4e5f6a7b8c9d0e1"),
      "tags": ["database", "nosql", "tutorial"],
      "createdAt": ISODate("2023-10-05T12:00:00Z")
      }
  • Collection: comments

    • Documents:
      {
      "_id": ObjectId("65a1b2c3d4e5f6a7b8c9d0e3"),
      "postId": ObjectId("65a1b2c3d4e5f6a7b8c9d0e2"),
      "userId": ObjectId("65a1b2c3d4e5f6a7b8c9d0e1"),
      "comment": "Great tutorial!",
      "createdAt": ISODate("2023-10-05T12:30:00Z")
      }

Why Use This Structure?​

  • Databases help you organize data by application or domain.
  • Collections group related data together (e.g., users, posts, comments).
  • Documents store individual records with flexible schemas.

Summary​

  • Database: A container for collections.
  • Collection: A group of documents (like a table in relational databases).
  • Document: A single record in BSON format (like a row in a table).

This structure makes MongoDB highly flexible and scalable, especially for applications with evolving data models.

JSON vs. BSON​

JSON (JavaScript Object Notation) and BSON (Binary JSON) are both data interchange formats, but they serve different purposes and have distinct characteristics. Here's a detailed comparison:


1. Definition​

  • JSON:

    • A lightweight, human-readable format for representing structured data.
    • Uses text-based key-value pairs and arrays.
    • Example:
      {
      "name": "John",
      "age": 30,
      "isStudent": false
      }
  • BSON:

    • A binary-encoded format for representing JSON-like documents.
    • Extends JSON with additional data types and optimizations for efficiency.
    • Example (in binary form, not human-readable).

2. Data Types​

  • JSON:

    • Supports basic data types:
      • Strings
      • Numbers
      • Booleans
      • Arrays
      • Objects
      • null
    • No support for advanced data types like dates or binary data.
  • BSON:

    • Extends JSON with additional data types:
      • Date
      • Binary data
      • ObjectId (used in MongoDB)
      • Timestamp
      • Regular expressions
      • 64-bit integers
    • Better suited for storing complex data.

3. Human-Readability​

  • JSON:

    • Human-readable and easy to write/understand.
    • Ideal for configuration files, APIs, and data exchange between systems.
  • BSON:

    • Not human-readable (binary format).
    • Designed for machine efficiency, not for direct human interaction.

4. Size and Efficiency​

  • JSON:

    • Text-based, so it can be larger in size compared to BSON.
    • Parsing and serialization can be slower due to its textual nature.
  • BSON:

    • Binary format, so it is more compact and efficient.
    • Faster to parse and serialize, making it ideal for high-performance applications.

5. Use Cases​

  • JSON:

    • Web APIs (e.g., REST APIs).
    • Configuration files.
    • Data exchange between client and server in web applications.
  • BSON:

    • MongoDB (used as the primary data storage format).
    • Applications requiring efficient storage and retrieval of complex data.
    • Scenarios where performance and compactness are critical.

6. Example Comparison​

  • JSON:

    {
    "name": "Alice",
    "age": 25,
    "birthdate": "1998-05-15",
    "isActive": true
    }
  • BSON:

    • The same data in BSON would include additional metadata and be stored in binary form, making it more efficient for storage and processing.

7. Pros and Cons​

FeatureJSONBSON
ReadabilityHuman-readableNot human-readable
Data TypesLimitedExtended (e.g., dates, binary)
SizeLarger (text-based)Smaller (binary)
PerformanceSlower parsing/serializationFaster parsing/serialization
Use CaseAPIs, config files, web appsDatabases, high-performance apps

When to Use Which?​

  • Use JSON when:

    • Human readability is important.
    • You're working with APIs or configuration files.
    • The data is simple and doesn't require advanced data types.
  • Use BSON when:

    • You need efficient storage and retrieval (e.g., in databases like MongoDB).
    • You're working with complex data types (e.g., dates, binary data).
    • Performance and compactness are critical.

In summary, JSON is great for human-readable data exchange, while BSON is optimized for performance and storage in systems like MongoDB.


Creating Databases & Collections​

In MongoDB, creating databases and collections is straightforward and can be done using the MongoDB Shell (mongosh), MongoDB Compass (GUI), or programmatically via a MongoDB driver (e.g., in Python, Node.js, etc.). Below is a step-by-step guide to creating databases and collections using the MongoDB Shell.


Step 1: Start MongoDB Shell​

  1. Open a terminal or command prompt.
  2. Run the following command to start the MongoDB Shell:
    mongosh
    This will connect to the default MongoDB instance running on localhost:27017.

Step 2: Create a Database​

  • MongoDB does not have an explicit "create database" command. Instead, a database is created when you first store data in it.
  • Use the use <database_name> command to switch to a database. If the database does not exist, MongoDB will create it.

Example:​

use school
  • This switches to the school database. If school does not exist, it will be created.

Step 3: Create a Collection​

  • Similar to databases, collections are created implicitly when you first insert a document into them.
  • You can also explicitly create a collection using the db.createCollection() method.

Example 1: Implicit Collection Creation​

  1. Insert a document into a collection:
    db.students.insertOne({ name: "John Doe", age: 20 })
    • This creates a collection named students in the school database (if it doesn’t already exist) and inserts a document.

Example 2: Explicit Collection Creation​

  1. Use the db.createCollection() method to create a collection:
    db.createCollection("teachers")
    • This creates an empty collection named teachers in the school database.

Step 4: Verify the Database and Collection​

  1. List all databases:

    show dbs
    • This will display all databases. Note that a database will only appear here after it contains data.
  2. List collections in the current database:

    show collections
    • This will display all collections in the school database (e.g., students, teachers).
  3. View documents in a collection:

    db.students.find()
    • This will display all documents in the students collection.

Step 5: Switch Between Databases​

  • Use the use <database_name> command to switch between databases.

Example:​

use ecommerce
  • This switches to the ecommerce database. If it doesn’t exist, it will be created when you insert data.

Step 6: Drop a Database or Collection​

  • To delete a database:

    db.dropDatabase()
    • This deletes the current database.
  • To delete a collection:

    db.collectionName.drop()
    • Example:
      db.students.drop()
    • This deletes the students collection.

Example Workflow​

  1. Create a database:

    use school
  2. Create a collection explicitly:

    db.createCollection("students")
  3. Insert a document:

    db.students.insertOne({ name: "Jane Doe", age: 22 })
  4. Verify the data:

    show dbs
    show collections
    db.students.find()

Using MongoDB Compass (GUI)​

If you prefer a graphical interface:

  1. Open MongoDB Compass.
  2. Connect to your MongoDB instance.
  3. Click Create Database and provide a database name and collection name.
  4. Insert documents using the Insert Document button.

Using a Programming Language (e.g., Python)​

Here’s an example using the pymongo driver in Python:

from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017")

# Create or switch to a database
db = client.school

# Create or switch to a collection
collection = db.students

# Insert a document
collection.insert_one({ "name": "Alice", "age": 25 })

# Fetch and print documents
for doc in collection.find():
print(doc)

Summary​

  • Use use <database_name> to create or switch to a database.
  • Use db.createCollection("<collection_name>") to create a collection explicitly.
  • Insert documents using db.collectionName.insertOne() or db.collectionName.insertMany().
  • Use show dbs, show collections, and db.collectionName.find() to verify your data.

This process is simple and flexible, making MongoDB easy to work with for both beginners and advanced users.