MongoDB Assignment- 3

Basic Questions

  1. Run an aggregation with $match + $project to show why aggregation is needed vs normal find.
  2. Write a simple aggregation query with $match to filter students with grade “A”.
  3. Use $group on students collection to count how many students are in each grade.
  4. Use $sort to arrange students by age in ascending order.
  5. Write an aggregation pipeline using $project to return only name and grade.
  6. Write an aggregation with $limit to show only the first 3 students.
  7. Demonstrate $skip in an aggregation query.
  8. Write both a .find() and equivalent aggregation on students to compare results.
  9. Give an example of $sum in aggregation to calculate the total marks of a student.
  10. How do you calculate the average age of students using aggregation?
  11. Write a query using $max to find the maximum marks among students.
  12. Write a query using $min to find the youngest student’s age.
  13. Create an index on age field and run .explain(“executionStats”) to show faster query.
  14. Write a command to create an index on the name field in the students collection.
  15. How do you list all indexes of a collection?
  16. Write a command to drop an index.
  17. Create a compound index on { grade:1, age:-1 } and run a query using both fields.
  18. Insert documents with an array field and create a multikey index on it.
  19. Create two collections authors & books — one with embedding and one with referencing — then query both.
  20. Insert two collections users & profiles in 1:1 relation and query profile details by user.

Intermediate Questions

  1. Write an aggregation query with $match and $group to count students in each grade.
  2. Write an aggregation query to calculate the average salary of employees per department.
  3. Write an aggregation query with $project to display a new field “fullName” combining firstName and lastName.
  4. Implement pagination using $skip + $limit in aggregation to fetch students page 2 (5 per page).
  5. Write an aggregation pipeline to filter students by grade “A”, then sort by marks.
  6. Use $lookup to join students collection with courses.
  7. Write a query using $lookup to combine students with their course details.
  8. Use aggregation expressions $sum and $avg within group to calculate total & average marks.
  9. Create an index and run query with .explain() to measure performance improvement.
  10. Show an example of creating a compound index on { grade: 1, age: -1 }.
  11. Add subjects:[“Math”,”Science”] to docs and show why multikey index is needed for array queries.
  12. Write a query to demonstrate how an index can be used with explain(“executionStats”).
  13. Store data once with embedding, once with referencing, then query both to compare performance.
  14. Create schema for one‑to‑many (students → courses), insert sample and query.
  15. Insert polymorphic docs (different fields for videoPost vs textPost) in same collection and query them.
  16. Apply a JSON Schema validator in collection creation (age must be > 18).
  17. Write a JSON Schema validation rule to ensure “age” must be a number greater than 18.
  18. Create two collections with different validators: one with validationAction:warn, another validationAction:error. Test inserts.
  19. Model many‑to‑many relation: students ↔ teachers using array of ObjectIds + run a query joining them.
  20. Insert two variations of schema designs for the same relation and query, to illustrate best practice.

Advanced Questions

  1. Write a multi-stage aggregation pipeline with $match, $group, and $sort to display top 3 highest paid employees in each department.
  2. Write an aggregation to calculate total sales per month and year using $group.
  3. Write an aggregation to calculate the average marks per subject per student.
  4. Write an aggregation using $lookup and $unwind to flatten a one-to-many relationship.
  5. Design customers, orders, products collections mixing embedding (order items) and referencing (customerId), then insert/query.
  6. Write a JSON Schema validation that enforces “email” must be a string and follow a valid email regex.
  7. Simulate transaction‑like behavior by using two phase commits in single document update.
  8. Create too many indexes on small dataset and measure slower insert performance.
  9. Write an aggregation query with $facet to return both total count and paginated results in a single query.
  10. Build collections: users, posts, comments. Insert nested relationships then query all comments for a user’s posts with $lookup.