MongoDB Assignment- 3
Basic Questions
- Run an aggregation with $match + $project to show why aggregation is needed vs normal find.
- Write a simple aggregation query with $match to filter students with grade “A”.
- Use $group on students collection to count how many students are in each grade.
- Use $sort to arrange students by age in ascending order.
- Write an aggregation pipeline using $project to return only name and grade.
- Write an aggregation with $limit to show only the first 3 students.
- Demonstrate $skip in an aggregation query.
- Write both a .find() and equivalent aggregation on students to compare results.
- Give an example of $sum in aggregation to calculate the total marks of a student.
- How do you calculate the average age of students using aggregation?
- Write a query using $max to find the maximum marks among students.
- Write a query using $min to find the youngest student’s age.
- Create an index on age field and run .explain(“executionStats”) to show faster query.
- Write a command to create an index on the name field in the students collection.
- How do you list all indexes of a collection?
- Write a command to drop an index.
- Create a compound index on { grade:1, age:-1 } and run a query using both fields.
- Insert documents with an array field and create a multikey index on it.
- Create two collections authors & books — one with embedding and one with referencing — then query both.
- Insert two collections users & profiles in 1:1 relation and query profile details by user.
Intermediate Questions
- Write an aggregation query with $match and $group to count students in each grade.
- Write an aggregation query to calculate the average salary of employees per department.
- Write an aggregation query with $project to display a new field “fullName” combining firstName and lastName.
- Implement pagination using $skip + $limit in aggregation to fetch students page 2 (5 per page).
- Write an aggregation pipeline to filter students by grade “A”, then sort by marks.
- Use $lookup to join students collection with courses.
- Write a query using $lookup to combine students with their course details.
- Use aggregation expressions $sum and $avg within group to calculate total & average marks.
- Create an index and run query with .explain() to measure performance improvement.
- Show an example of creating a compound index on { grade: 1, age: -1 }.
- Add subjects:[“Math”,”Science”] to docs and show why multikey index is needed for array queries.
- Write a query to demonstrate how an index can be used with explain(“executionStats”).
- Store data once with embedding, once with referencing, then query both to compare performance.
- Create schema for one‑to‑many (students → courses), insert sample and query.
- Insert polymorphic docs (different fields for videoPost vs textPost) in same collection and query them.
- Apply a JSON Schema validator in collection creation (age must be > 18).
- Write a JSON Schema validation rule to ensure “age” must be a number greater than 18.
- Create two collections with different validators: one with validationAction:warn, another validationAction:error. Test inserts.
- Model many‑to‑many relation: students ↔ teachers using array of ObjectIds + run a query joining them.
- Insert two variations of schema designs for the same relation and query, to illustrate best practice.
Advanced Questions
- Write a multi-stage aggregation pipeline with $match, $group, and $sort to display top 3 highest paid employees in each department.
- Write an aggregation to calculate total sales per month and year using $group.
- Write an aggregation to calculate the average marks per subject per student.
- Write an aggregation using $lookup and $unwind to flatten a one-to-many relationship.
- Design customers, orders, products collections mixing embedding (order items) and referencing (customerId), then insert/query.
- Write a JSON Schema validation that enforces “email” must be a string and follow a valid email regex.
- Simulate transaction‑like behavior by using two phase commits in single document update.
- Create too many indexes on small dataset and measure slower insert performance.
- Write an aggregation query with $facet to return both total count and paginated results in a single query.
- Build collections: users, posts, comments. Insert nested relationships then query all comments for a user’s posts with $lookup.