1. Examples

Example 1: Library Book Collection

You’re tasked with creating a simple Library Book Collection to manage information about books in a library. Using MongoDB and a GUI like MongoDB Compass or the Mongo Shell, build the system step-by-step.

Requirements and Tasks

Requirements

  • Your database must store the following book data:
TitleAuthorGenrePublished Year
The Great GatsbyF. Scott FitzgeraldFiction1925
To Kill a MockingbirdHarper LeeFiction1960
1984George OrwellDystopian1949

Tasks

  1. Create a collection and insert the book data
    // Connect to MongoDB
    use LibraryCollection;
    
    // Create a collection for books and insert data
    db.books.insertMany([
        {
            title: "The Great Gatsby",
            author: "F. Scott Fitzgerald",
            genre: "Fiction",
            publishedYear: 1925
        },
        {
            title: "To Kill a Mockingbird",
            author: "Harper Lee",
            genre: "Fiction",
            publishedYear: 1960
        },
        {
            title: "1984",
            author: "George Orwell",
            genre: "Dystopian",
            publishedYear: 1949
        }
    ]);
  2. Write MongoDB queries to do the following:

    • Retrieve the titles of all books
      db.books.find({}, { title: 1, _id: 0 });

      Explanation:

      • db.books.find() is querying the books collection.
      • {}: The empty object is the filter criteria, which means “select all documents” (no filter applied).
      • { title: 1, _id: 0 }: This is the projection. It includes only the title field in the result (1 means include), and excludes the _id field (0 means exclude).
    • Retrieve the authors of all books published after 1950
      db.books.find(
          { publishedYear: { $gt: 1950 } },
          { author: 1, _id: 0 }
      );

      Explanation:

      • { publishedYear: { $gt: 1950 } }: This is the filter condition. $gt stands for “greater than”, so this will find books published after 1950.
      • { author: 1, _id: 0 }: The projection includes only the author field and excludes the _id field from the results.
    • Retrieve all books of the "Fiction" genre
      db.books.find(
          { genre: "Fiction" }
      );

      Explanation:

      • { genre: "Fiction" }: This filter condition finds all books where the genre is exactly “Fiction”.
      • No projection is specified, so MongoDB will return all fields for the matching documents by default.
  3. Update the published year of "1984" to 1950
    db.books.updateOne(
        { title: "1984" },
        { $set: { publishedYear: 1950 } }
    );

    Explanation:

    • db.books.updateOne(): This updates a single document in the books collection.
    • { title: "1984" }: The filter to find the document with the title “1984”.
    • { $set: { publishedYear: 1950 } }: The $set operator updates the publishedYear field to 1950. If the field doesn’t exist, it will be added.
  4. Remove "To Kill a Mockingbird" from the collection
    db.books.deleteOne({ title: "To Kill a Mockingbird" });

    Explanation:

    • db.books.deleteOne(): This deletes a single document from the books collection.
    • { title: "To Kill a Mockingbird" }: The filter condition that matches the document where the title is “To Kill a Mockingbird”.

Example 2: Movie Collection

You’re tasked with creating a simple Movie Collection to manage information about movies in a database. Using MongoDB and a GUI like MongoDB Compass or the Mongo Shell, build the system step-by-step.

Requirements and Tasks

Requirements

  • Your database must store the following movie data:
TitleDirectorGenreRelease Year
InceptionChristopher NolanSci-Fi2010
The Dark KnightChristopher NolanAction2008
InterstellarChristopher NolanSci-Fi2014

Tasks

  1. Create a collection and insert the movie data
    // Connect to MongoDB
    use MovieCollection;
    
    // Create a collection for movies and insert data
    db.movies.insertMany([
        {
            title: "Inception",
            director: "Christopher Nolan",
            genre: "Sci-Fi",
            releaseYear: 2010
        },
        {
            title: "The Dark Knight",
            director: "Christopher Nolan",
            genre: "Action",
            releaseYear: 2008
        },
        {
            title: "Interstellar",
            director: "Christopher Nolan",
            genre: "Sci-Fi",
            releaseYear: 2014
        }
    ]);
  2. Write MongoDB queries to do the following:

    • Retrieve the titles of all movies
      db.movies.find({}, { title: 1, _id: 0 });

      Explanation:

      • db.movies.find() queries the movies collection.
      • {}: The empty filter matches all documents in the collection.
      • { title: 1, _id: 0 }: The projection includes the title field and excludes the _id field in the result.
    • Retrieve the directors of all movies released after 2010
      db.movies.find(
          { releaseYear: { $gt: 2010 } },
          { director: 1, _id: 0 }
      );

      Explanation:

      • { releaseYear: { $gt: 2010 } }: This filter finds movies released after 2010 ($gt stands for “greater than”).
      • { director: 1, _id: 0 }: The projection includes the director field and excludes the _id field.
    • Retrieve all movies of the "Sci-Fi" genre
      db.movies.find(
          { genre: "Sci-Fi" }
      );

      Explanation:

      • { genre: "Sci-Fi" }: This filter finds movies where the genre field is “Sci-Fi”.
      • No projection is specified, so MongoDB will return all fields for the matching documents by default.
  3. Update the release year of "Interstellar" to 2015
    db.movies.updateOne(
        { title: "Interstellar" },
        { $set: { releaseYear: 2015 } }
    );

    Explanation:

    • db.movies.updateOne(): This updates a single document in the movies collection.
    • { title: "Interstellar" }: The filter condition selects the movie “Interstellar”.
    • { $set: { releaseYear: 2015 } }: The $set operator updates the releaseYear to 2015.
  4. Remove "The Dark Knight" from the collection
    db.movies.deleteOne({ title: "The Dark Knight" });

    Explanation:

    • db.movies.deleteOne(): This deletes a single document from the movies collection.
    • { title: "The Dark Knight" }: The filter condition specifies that the document with the title “The Dark Knight” should be deleted.

Example 3: School Enrollment System

You’re tasked with creating a simple School Enrollment System to manage students, courses, and their enrollments in those courses. Using MongoDB and a GUI like MongoDB Compass or the Mongo Shell, build the system step-by-step.

Requirements and Tasks

Requirements

  • Your database must store the following student data:
First NameLast NameEmailSchool Enrollment Date
AliceJohnsonalice.johnson@example.comSeptember 15, 2024
BobSmithbob.smith@example.comSeptember 16, 2024
CharlieWilliamscharlie.williams@example.comSeptember 17, 2024
  • Your database must store the following course data:
Course NameCourse Description
Physics 101Introduction to Physics
Literature 201Basics of World Literature
History 101A Survey of Historical Events
  • Your database must store the following course enrollments:
Student NameCourse NameEnrollment Date
Alice JohnsonPhysics 101September 18, 2024
Alice JohnsonLiterature 201September 18, 2024
Bob SmithHistory 101September 19, 2024

Tasks

  1. Create the collections to store all of this data
       // Create a collection for students
       db.students.insertMany([
           {
               firstName: "Alice",
               lastName: "Johnson",
               email: "alice.johnson@example.com",
               enrollmentDate: new Date("2024-09-15")
           },
           {
               firstName: "Bob",
               lastName: "Smith",
               email: "bob.smith@example.com",
               enrollmentDate: new Date("2024-09-16")
           },
           {
               firstName: "Charlie",
               lastName: "Williams",
               email: "charlie.williams@example.com",
               enrollmentDate: new Date("2024-09-17")
           }
       ]);
    
       // Create a collection for courses
       db.courses.insertMany([
           {
               courseName: "Physics 101",
               courseDescription: "Introduction to Physics"
           },
           {
               courseName: "Literature 201",
               courseDescription: "Basics of World Literature"
           },
           {
               courseName: "History 101",
               courseDescription: "A Survey of Historical Events"
           }
       ]);
    
       // Create a collection for enrollments
       db.enrollments.insertMany([
           {
               studentName: "Alice Johnson",
               courseName: "Physics 101",
               enrollmentDate: new Date("2024-09-18")
           },
           {
               studentName: "Alice Johnson",
               courseName: "Literature 201",
               enrollmentDate: new Date("2024-09-18")
           },
           {
               studentName: "Bob Smith",
               courseName: "History 101",
               enrollmentDate: new Date("2024-09-19")
           }
       ]);

    Note: We use three separate collections (students, courses, and enrollments) instead of combining all the data into one large collection because:

    • Separation of Concerns: Each collection has a specific purpose and avoids duplication of data. For example, the course details do not need to be repeated for every enrollment.
    • Flexibility: Adding or updating data in one collection (e.g., adding a new course) doesn’t require changes to unrelated data (e.g., students).
    • Performance: Queries targeting specific types of data (e.g., retrieving all courses) are faster because they don’t need to filter unrelated fields in a large collection.
  2. Write MongoDB queries to do the following:

    • Retrieve the full names of all students
      db.students.find({}, { firstName: 1, lastName: 1, _id: 0 });
    • Retrieve the course names of all of "Bob Smith"'s courses
      db.enrollments.find(
          { studentName: "Bob Smith" },
          { courseName: 1, _id: 0 }
      );
    • Retrieve all students enrolled in "Physics 101"
      db.enrollments.find(
          { courseName: "Physics 101" },
          { studentName: 1, _id: 0 }
      );
  3. Change "Charlie William"'s email to charlie.w.newemail@example.com
    db.students.updateOne(
        { firstName: "Charlie", lastName: "Williams" },
        { $set: { email: "charlie.w.newemail@example.com" } }
    );
  4. Remove "Alice Johnson" from "Literature 201"
    db.enrollments.deleteOne({
        studentName: "Alice Johnson",
        courseName: "Literature 201"
    });

Example 4: Car Rental System

You’re tasked with creating a Car Rental System to manage rental cars, customers, and their rental records. Using MongoDB and a GUI like MongoDB Compass or the Mongo Shell, build the system step-by-step.

Requirements and Tasks

Requirements

Your database must store the following car data:

Car ModelBrandYearTypePrice Per Day
CivicHonda2020Sedan40
CamryToyota2019Sedan45
Model 3Tesla2022Electric100

Your database must store the following customer data:

First NameLast NameEmailLicense Number
JohnDoejohn.doe@example.comD123456
SarahLeesarah.lee@example.comS654321
MichaelSmithmichael.smith@example.comM789012

Your database must store the following rental records:

Customer NameCar ModelRental Start DateRental End Date
John DoeCivic2024-03-012024-03-05
Sarah LeeModel 32024-03-102024-03-12
Michael SmithCamry2024-03-152024-03-20

Tasks

  1. Create the collections to store all of this data
    // Create a collection for cars
    db.cars.insertMany([
        { model: "Civic", brand: "Honda", year: 2020, type: "Sedan", pricePerDay: 40 },
        { model: "Camry", brand: "Toyota", year: 2019, type: "Sedan", pricePerDay: 45 },
        { model: "Model 3", brand: "Tesla", year: 2022, type: "Electric", pricePerDay: 100 }
    ]);
    
    // Create a collection for customers
    db.customers.insertMany([
        { firstName: "John", lastName: "Doe", email: "john.doe@example.com", licenseNumber: "D123456" },
        { firstName: "Sarah", lastName: "Lee", email: "sarah.lee@example.com", licenseNumber: "S654321" },
        { firstName: "Michael", lastName: "Smith", email: "michael.smith@example.com", licenseNumber: "M789012" }
    ]);
    
    // Create a collection for rental records
    db.rentals.insertMany([
        { customerName: "John Doe", carModel: "Civic", rentalStart: new Date("2024-03-01"), rentalEnd: new Date("2024-03-05") },
        { customerName: "Sarah Lee", carModel: "Model 3", rentalStart: new Date("2024-03-10"), rentalEnd: new Date("2024-03-12") },
        { customerName: "Michael Smith", carModel: "Camry", rentalStart: new Date("2024-03-15"), rentalEnd: new Date("2024-03-20") }
    ]);
  2. Write MongoDB queries to do the following:

    • Retrieve all available cars (cars that are not currently rented)
      db.cars.find({
          model: { $nin: db.rentals.distinct("carModel") }
      });

      Explanation:

      • db.rentals.distinct("carModel"): Gets all rented car models.
      • $nin: Finds cars whose models are not in the rented list.
    • Retrieve the full names of all customers
      db.customers.aggregate([
          {
              $project: {
                  _id: 0,
                  fullName: { $concat: ["$firstName", " ", "$lastName"] }
              }
          }
      ]);
    • Find the total rental days for each customer
      db.rentals.aggregate([
          {
              $project: {
                  customerName: 1,
                  totalDays: { 
                      $dateDiff: { 
                          startDate: "$rentalStart", 
                          endDate: "$rentalEnd", 
                          unit: "day" 
                      }
                  }
              }
          }
      ]);

      Explanation:

      • $dateDiff: Computes the difference between rentalStart and rentalEnd in days.
    • Find the total earnings for each car model
      db.rentals.aggregate([
          {
              $lookup: {
                  from: "cars",
                  localField: "carModel",
                  foreignField: "model",
                  as: "carDetails"
              }
          },
          { $unwind: "$carDetails" },
          {
              $project: {
                  carModel: 1,
                  totalRevenue: {
                      $multiply: [
                          { 
                              $dateDiff: { 
                                  startDate: "$rentalStart", 
                                  endDate: "$rentalEnd", 
                                  unit: "day" 
                              }
                          },
                          "$carDetails.pricePerDay"
                      ]
                  }
              }
          },
          { $group: { _id: "$carModel", totalEarnings: { $sum: "$totalRevenue" } } }
      ]);

      Explanation:

      • $lookup: Joins rentals with the cars collection.
      • $unwind: Flattens the array from $lookup.
      • $multiply: Multiplies the number of rental days by pricePerDay.
      • $group: Sums up total earnings per car.
    • Retrieve all rental records for "John Doe"
      db.rentals.find(
          { customerName: "John Doe" }
      );
  3. Change "Sarah Lee"'s email to s.lee@example.com
    db.customers.updateOne(
        { firstName: "Sarah", lastName: "Lee" },
        { $set: { email: "s.lee@example.com" } }
    );
  4. Remove "Michael Smith"'s rental record
    db.rentals.deleteOne({
        customerName: "Michael Smith"
    });