MCQs Of Day 15: MongoDB Aggregation Operators
MCQs on MongoDB Aggregation Operators
1. What is the purpose of the $group operator in MongoDB's aggregation framework?
o A) To filter documents based on a condition.
o B) To sort documents by a field.
o C) To group documents based on a field and perform aggregation.
o D) To create new documents based on existing ones.
Answer: C) To group documents based on a field and perform aggregation.
Explanation: The $group operator groups documents by a specified field and allows you to perform various aggregation operations like sum, average, etc., on the grouped data.
2. Which operator is used to calculate the sum of values in MongoDB aggregation?
o A) $avg
o B) $sum
o C) $count
o D) $multiply
Answer: B) $sum
Explanation: The $sum operator calculates the sum of values in a given field.
3. What does the $avg operator in MongoDB aggregation do?
o A) Calculates the average of a field's values.
o B) Groups data based on field values.
o C) Adds a specified value to all documents.
o D) Counts the number of documents in a collection.
Answer: A) Calculates the average of a field's values.
Explanation: The $avg operator calculates the average value of a specific field within a group.
4. Which operator is used to count the number of documents in MongoDB aggregation?
o A) $count
o B) $sum
o C) $avg
o D) $min
Answer: A) $count
Explanation: The $count operator counts the number of documents within a group or collection.
5. In which stage of the aggregation pipeline is the $group operator typically used?
o A) $match
o B) $project
o C) $group
o D) $sort
Answer: C) $group
Explanation: The $group operator is used in the aggregation pipeline to group documents and apply aggregation functions.
6. Which of the following can be an output of the $group operator in MongoDB?
o A) A single document with the sum of all field values.
o B) A list of documents, each representing a group.
o C) The average value of a field.
o D) A collection of documents sorted by a field.
Answer: B) A list of documents, each representing a group.
Explanation: The $group operator returns a list of documents, where each document represents a unique group defined by the _id field.
7. What is the purpose of the $match stage in MongoDB’s aggregation pipeline?
o A) To filter documents before the aggregation starts.
o B) To group documents based on a field.
o C) To calculate values for each group.
o D) To display the results of the aggregation.
Answer: A) To filter documents before the aggregation starts.
Explanation: The $match stage filters documents based on specified conditions, similar to a WHERE clause in SQL.
8. Which of the following is a valid way to sum the product of two fields, price and quantity, in MongoDB aggregation?
o A) $sum: ["$price", "$quantity"]
o B) $sum: { $multiply: ["$price", "$quantity"] }
o C) $sum: { $add: ["$price", "$quantity"] }
o D) $sum: { $divide: ["$price", "$quantity"] }
Answer: B) $sum: { $multiply: ["$price", "$quantity"] }
Explanation: To sum the product of two fields, $multiply is used inside $sum to perform multiplication before summing the values.
9. In MongoDB, how do you find the total number of orders placed in each department?
o A) Use $group with $count.
o B) Use $group with $sum.
o C) Use $project with $count.
o D) Use $match with $count.
Answer: A) Use $group with $count.
Explanation: To count orders by department, you can use $group to group by department and $count to count the number of orders in each group.
10. Which aggregation operator can be used to group documents based on multiple fields?
- A) $project
- B) $group
- C) $sort
- D) $unwind
Answer: B) $group
Explanation: The $group operator allows you to group documents based on multiple fields by specifying each field as part of the _id field.
11. Which operator would you use to calculate the average salary for employees in each department?
- A) $avg
- B) $sum
- C) $group
- D) $project
Answer: A) $avg
Explanation: The $avg operator is used to calculate the average value of a specified field, such as salary.
12. How can you group documents by month in MongoDB?
- A) Use $group with $month.
- B) Use $match with $month.
- C) Use $project with $month.
- D) Use $unwind with $month.
Answer: A) Use $group with $month.
Explanation: You can extract the month from a date field and group the documents by it using $group along with the $month operator.
13. What is the result of the $count operator in MongoDB aggregation?
- A) The total sum of a field.
- B) The number of documents in the collection.
- C) The number of documents after filtering.
- D) The average of a specified field.
Answer: C) The number of documents after filtering.
Explanation: The $count operator counts the number of documents that remain after the aggregation pipeline, optionally filtering with $match.
14. How do you calculate the total revenue for each product category?
- A) Use $group and $sum on category and revenue.
- B) Use $group and $avg on category and revenue.
- C) Use $project and $sum on category.
- D) Use $sort and $sum on category.
Answer: A) Use $group and $sum on category and revenue.
Explanation: $group groups the data by category, and $sum calculates the total revenue for each category.
15. Which stage should be used to reshape the documents and create new computed fields in MongoDB?
- A) $match
- B) $group
- C) $project
- D) $sort
Answer: C) $project
Explanation: The $project stage reshapes documents by including, excluding, or creating new computed fields.
16. What does the $unwind operator do in MongoDB aggregation?
- A) Combines multiple documents into one.
- B) Breaks an array into multiple documents.
- C) Groups documents based on array values.
- D) Sorts documents in ascending order.
Answer: B) Breaks an array into multiple documents.
Explanation: The $unwind operator is used to deconstruct an array field from the input documents, generating a new document for each element.
17. Which operator can be used to calculate the minimum value of a field in a group?
- A) $min
- B) $sum
- C) $avg
- D) $max
Answer: A) $min
Explanation: The $min operator calculates the minimum value of a specified field within a group.
18. How do you filter documents to include only those where the status is "delivered"?
- A) Use $match with {status: "delivered"}.
- B) Use $group with {status: "delivered"}.
- C) Use $count with {status: "delivered"}.
- D) Use $project with {status: "delivered"}.
Answer: A) Use $match with {status: "delivered"}.
Explanation: The $match stage filters documents based on a condition, such as status: "delivered".
19. In MongoDB, how can you compute the total revenue from multiple product orders?
- A) Use $count with amount.
- B) Use $sum with quantity * price.
- C) Use $avg with quantity.
- D) Use $project with amount.
Answer: B) Use $sum with quantity * price.
Explanation: To compute total revenue, multiply quantity and price and use $sum to calculate the total.
20. What operator would you use to exclude a field from the aggregation result in MongoDB?
- A) $exclude
- B) $project
- C) $match
- D) $filter
Answer: B) $project
Explanation: The $project operator can be used to include or exclude specific fields from the output documents.
[The list continues to include 30 more MCQs. Please let me know if you'd like the full list to be continued.]
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the remaining 30 multiple-choice questions (MCQs) based on MongoDB Aggregation Operators with explanations:
21. Which aggregation operator is used to calculate the maximum value of a field within a group?
- A) $min
- B) $max
- C) $avg
- D) $sum
Answer: B) $max
Explanation: The $max operator calculates the maximum value of a specified field within a group.
22. How would you use the $project stage to exclude a field called "address" in the output?
- A) { address: 0 }
- B) { address: 1 }
- C) { _id: 0, address: 0 }
- D) { _id: 1, address: 0 }
Answer: A) { address: 0 }
Explanation: In $project, specifying a field with 0 excludes it from the result.
23. Which operator is used to multiply two fields in MongoDB aggregation?
- A) $multiply
- B) $sum
- C) $subtract
- D) $divide
Answer: A) $multiply
Explanation: The $multiply operator is used to multiply two fields or values in an aggregation pipeline.
24. How do you extract the year from a date field using MongoDB aggregation?
- A) $year
- B) $dateToString
- C) $dateFromParts
- D) $extract
Answer: A) $year
Explanation: The $year operator extracts the year from a date field.
25. What is the result of using $count in an aggregation pipeline?
- A) A list of all documents.
- B) The number of documents in the collection.
- C) The number of documents passing the pipeline conditions.
- D) The sum of all field values.
Answer: C) The number of documents passing the pipeline conditions.
Explanation: The $count operator counts how many documents are processed after all previous pipeline stages.
26. How can you combine multiple fields into one value in MongoDB aggregation?
- A) $group
- B) $concat
- C) $addFields
- D) $combine
Answer: B) $concat
Explanation: The $concat operator combines multiple string fields into a single string value.
27. What will the following aggregation pipeline return: {$group: {_id: null, totalPrice: {$sum: "$price"}}}?
- A) The average price of all products.
- B) The total price of all products in the collection.
- C) The price of the first product.
- D) The total number of products.
Answer: B) The total price of all products in the collection.
Explanation: The $sum operator sums the values in the price field across all documents.
28. What does the $match stage do in the MongoDB aggregation pipeline?
- A) It filters documents based on specified criteria.
- B) It groups documents together.
- C) It calculates the sum of a field.
- D) It reshapes documents to include/exclude fields.
Answer: A) It filters documents based on specified criteria.
Explanation: The $match stage filters documents just like a WHERE clause in SQL.
29. Which of the following would be the correct use of $sum to calculate the total quantity sold for each product?
- A) $sum: "$quantity"
- B) $sum: { $multiply: ["$quantity", "$price"] }
- C) { $sum: { $add: ["$quantity", "$price"] } }
- D) $sum: { $avg: "$quantity" }
Answer: A) $sum: "$quantity"
Explanation: $sum can directly sum up the values of a specified field like quantity for each group.
30. How do you calculate the total revenue for each department by summing the salary field using MongoDB aggregation?
- A) Use $avg to calculate the average salary per department.
- B) Use $sum to calculate the total salary for each department.
- C) Use $count to count employees per department.
- D) Use $project to display salary values.
Answer: B) Use $sum to calculate the total salary for each department.
Explanation: The $sum operator calculates the total sum of the salary field within each department group.
31. What is the purpose of the $project stage in MongoDB aggregation?
- A) To group documents.
- B) To filter documents.
- C) To reshape documents and create new computed fields.
- D) To count the number of documents.
Answer: C) To reshape documents and create new computed fields.
Explanation: The $project stage reshapes documents, either by including, excluding, or transforming fields.
32. How do you calculate the number of orders with status: "completed"?
- A) Use $group and $count.
- B) Use $match followed by $count.
- C) Use $project and $sum.
- D) Use $count alone.
Answer: B) Use $match followed by $count.
Explanation: First, filter the orders with $match, then use $count to count the number of documents.
33. Which of the following would use $group with multiple fields?
- A) {$group: {_id: "$category", totalAmount: {$sum: "$amount"}}}
- B) {$group: {_id: "$category", totalAmount: {$sum: "$amount"}, avgAmount: {$avg: "$amount"}}}
- C) {$group: {_id: "$status"}}
- D) {$group: {_id: null, totalAmount: {$sum: "$amount"}}}
Answer: B) {$group: {_id: "$category", totalAmount: {$sum: "$amount"}, avgAmount: {$avg: "$amount"}}}
Explanation: You can group by multiple fields, and $group can include multiple aggregations like $sum and $avg.
34. How do you aggregate values over specific date ranges in MongoDB?
- A) Use $year operator in $group.
- B) Use $match to filter documents by date.
- C) Use $dateToString to extract specific date components.
- D) All of the above.
Answer: D) All of the above.
Explanation: You can use $year, $dateToString, and $match to filter and group data based on specific date components.
35. Which operator is used to compute the median in MongoDB aggregation?
- A) $avg
- B) $median
- C) $percentile
- D) MongoDB does not have a native operator for median.
Answer: D) MongoDB does not have a native operator for median.
Explanation: MongoDB doesn’t have a $median operator; you would need to sort the data and calculate the median manually.
36. Which of the following stages can be used to limit the number of documents returned in MongoDB aggregation?
- A) $group
- B) $limit
- C) $match
- D) $project
Answer: B) $limit
Explanation: The $limit operator is used to restrict the number of documents in the result set.
37. Which operator is used to add a new field to the documents in MongoDB aggregation?
- A) $project
- B) $addFields
- C) $group
- D) $unwind
Answer: B) $addFields
Explanation: The $addFields operator is used to add new fields to the documents.
38. What does the $unwind operator do in MongoDB aggregation?
- A) Groups array values into a single document.
- B) Creates a new array from individual elements.
- C) Deconstructs an array field into individual documents.
- D) Sorts an array field in ascending order.
Answer: C) Deconstructs an array field into individual documents.
Explanation: The $unwind operator splits each element of an array into separate documents, each containing one element from the array.
39. What will the following aggregation query return: {$project: {totalPrice: {$multiply: ["$quantity", "$price"]}}}?
- A) The product of the quantity and price fields.
- B) A new document that contains quantity and price only.
- C) The sum of the quantity and price fields.
- D) The average of the quantity and price fields.
Answer: A) The product of the quantity and price fields.
Explanation: The $multiply operator calculates the product of quantity and price for each document.
40. Which operator can you use to perform a calculation based on a field value in the $group stage?
- A) $set
- B) $cond
- C) $multiply
- D) $dateToString
Answer: C) $multiply
Explanation: You can use $multiply to perform arithmetic calculations within the $group stage.
41. Which of the following operators is used for conditional statements in MongoDB aggregation?
- A) $case
- B) $if
- C) $cond
- D) $choose
Answer: C) $cond
Explanation: The $cond operator is used for conditional logic, similar to an if statement in programming languages.
42. What does the following query do: {$group: {_id: null, totalOrders: {$sum: 1}}}?
- A) It sums the total of all orders in the collection.
- B) It counts the total number of documents in the collection.
- C) It sums the total value of amount field for each order.
- D) It averages the values of orders.
Answer: B) It counts the total number of documents in the collection.
Explanation: Using $sum: 1 counts the number of documents in the collection.
43. How can you perform calculations like sum and average for a subset of documents?
- A) Use $count to filter documents first.
- B) Use $match followed by $group.
- C) Use $sort before $sum.
- D) Use $limit before $group.
Answer: B) Use $match followed by $group.
Explanation: Use $match to filter the data and $group to perform aggregations.
44. What is the role of the _id field in the $group operator?
- A) It defines the group key for the aggregation.
- B) It determines the number of documents.
- C) It stores the sum of all fields.
- D) It indicates the output field.
Answer: A) It defines the group key for the aggregation.
Explanation: The _id field in $group specifies the field by which documents will be grouped.
45. What will the $sort operator do in the aggregation pipeline?
- A) Filters documents based on a condition.
- B) Sorts the documents in ascending or descending order.
- C) Groups documents based on a specific field.
- D) Projects specified fields into the result.
Answer: B) Sorts the documents in ascending or descending order.
Explanation: The $sort operator sorts the documents by a specified field in either ascending or descending order.
46. Which operator is used to exclude a field from a document?
- A) $exclude
- B) $project with 0
- C) $match
- D) $sum
Answer: B) $project with 0
Explanation: The $project operator with 0 excludes a field from the result.
47. What does the $lookup operator do in MongoDB aggregation?
- A) Joins documents from two collections.
- B) Counts the number of documents in a collection.
- C) Filters documents based on a condition.
- D) Projects specific fields into the result.
Answer: A) Joins documents from two collections.
Explanation: The $lookup operator performs a left outer join between two collections.
48. How would you calculate the total revenue of orders from a specific year?
- A) Use $match with year condition and $sum.
- B) Use $group with year condition.
- C) Use $project to add revenue.
- D) Use $count with year condition.
Answer: A) Use $match with year condition and $sum.
Explanation: You can filter orders from a specific year using $match and then sum the revenue using $sum.
49. Which operator in MongoDB aggregation allows you to rename fields in the result?
- A) $rename
- B) $project
- C) $set
- D) $addFields
Answer: B) $project
Explanation: The $project stage can rename fields by specifying new names.
50. Which of the following is NOT a valid operator in the MongoDB aggregation pipeline?
- A) $match
- B) $group
- C) $filter
- D) $delete
Answer: D) $delete
Explanation: MongoDB’s aggregation framework does not have a $delete operator for removing documents. The correct operator for filtering documents is $match.
