MCQs Of Day 11: Aggregation Framework – Part 2 (Advanced)

Rashmi Mishra
0

 MCQs Of Day 11: Aggregation Framework – Part 2 (Advanced)

1. What does the $match stage in the MongoDB aggregation framework do?

  • A) Groups documents by a specified field
  • B) Filters documents based on specified criteria
  • C) Sorts documents in ascending order
  • D) Projects only specific fields from documents

Answer: B) Filters documents based on specified criteria
Explanation: The $match stage filters documents based on the conditions provided. It is similar to the WHERE clause in SQL.


2. What is the purpose of the $group stage in MongoDB aggregation?

  • A) To calculate the average of a field
  • B) To transform documents into key-value pairs
  • C) To group documents by a specified field
  • D) To project fields into a new structure

Answer: C) To group documents by a specified field
Explanation: The $group stage groups documents by a specified field and allows you to perform aggregation operations, such as sum, average, or count.


3. How do you calculate the total sum of a field using the aggregation pipeline?

  • A) $sum
  • B) $multiply
  • C) $add
  • D) $avg

Answer: A) $sum
Explanation: The $sum operator is used in the $group stage to calculate the total sum of a specified field.


4. Which aggregation operator is used to calculate the average of a field?

  • A) $avg
  • B) $sum
  • C) $multiply
  • D) $max

Answer: A) $avg
Explanation: The $avg operator is used to calculate the average of a specified field in the $group stage.


5. What is the role of the $project stage in MongoDB aggregation?

  • A) To filter documents based on certain conditions
  • B) To sort documents in a specific order
  • C) To reshape documents by including, excluding, or adding new fields
  • D) To calculate total sums or averages

Answer: C) To reshape documents by including, excluding, or adding new fields
Explanation: The $project stage is used to shape the output documents by selecting specific fields, excluding fields, or adding computed fields.


6. What does the $sort stage do in the MongoDB aggregation pipeline?

  • A) Filters documents based on a field
  • B) Groups documents by a specific field
  • C) Sorts documents in ascending or descending order
  • D) Computes new fields based on existing data

Answer: C) Sorts documents in ascending or descending order
Explanation: The $sort stage orders the documents based on the specified field(s), either in ascending (1) or descending (-1) order.


7. Which of the following aggregation stages can be used to exclude fields from documents?

  • A) $match
  • B) $group
  • C) $project
  • D) $sort

Answer: C) $project
Explanation: The $project stage can exclude fields by setting their values to 0, which removes the field from the result.


8. What will the following query do?

javascript

CopyEdit

db.orders.aggregate([

   { $match: { status: "shipped" } },

   { $project: { orderId: 1, totalAmount: 1 } }

])

  • A) It retrieves only the orderId and status fields for shipped orders.
  • B) It filters orders with status "shipped" and includes only orderId and totalAmount.
  • C) It groups orders by status and calculates the sum of totalAmount.
  • D) It filters orders with status "shipped" and calculates the average totalAmount.

Answer: B) It filters orders with status "shipped" and includes only orderId and totalAmount.
Explanation: The $match filters orders by status, and the $project includes only the orderId and totalAmount fields.


9. Which operator can be used in MongoDB to create a new field that is the product of two existing fields?

  • A) $multiply
  • B) $add
  • C) $avg
  • D) $sum

Answer: A) $multiply
Explanation: The $multiply operator can be used to multiply values from two fields and create a new computed field.


10. What is the output when you run this query?

javascript

CopyEdit

db.orders.aggregate([

   { $match: { status: "delivered" } },

   { $sort: { orderDate: 1 } }

])

  • A) It groups orders by status and sorts by totalAmount.
  • B) It retrieves orders with status "delivered" and sorts them by orderDate in ascending order.
  • C) It calculates the total sales for delivered orders.
  • D) It filters orders by status "delivered" and sorts by totalAmount in descending order.

Answer: B) It retrieves orders with status "delivered" and sorts them by orderDate in ascending order.
Explanation: The $match filters orders by status, and the $sort orders them by orderDate in ascending order.


11. How do you group documents by a field and calculate the sum of another field in MongoDB?

  • A) Using $group and $sum
  • B) Using $match and $add
  • C) Using $project and $avg
  • D) Using $sort and $multiply

Answer: A) Using $group and $sum
Explanation: The $group stage is used to group documents, and $sum calculates the total sum of a specified field.


12. What does the $limit stage do in the aggregation pipeline?

  • A) Limits the number of documents passed to the next stage
  • B) Groups documents by a field
  • C) Projects only selected fields
  • D) Filters documents based on a condition

Answer: A) Limits the number of documents passed to the next stage
Explanation: The $limit stage restricts the number of documents that continue through the aggregation pipeline.


13. What is the correct syntax to find the highest totalAmount in the orders collection?

  • A)

javascript

CopyEdit

db.orders.aggregate([{ $group: { _id: null, maxAmount: { $max: "$totalAmount" } } }])

  • B)

javascript

CopyEdit

db.orders.aggregate([{ $match: { totalAmount: { $max: "$totalAmount" } } }])

  • C)

javascript

CopyEdit

db.orders.aggregate([{ $project: { totalAmount: { $max: "$totalAmount" } } }])

  • D)

javascript

CopyEdit

db.orders.aggregate([{ $sort: { totalAmount: 1 } }])

Answer: A)

javascript

CopyEdit

db.orders.aggregate([{ $group: { _id: null, maxAmount: { $max: "$totalAmount" } } }])

Explanation: The $group stage is used to calculate the maximum value of totalAmount using $max.


14. Which operator is used to calculate the total sales for a specific category?

  • A) $sum
  • B) $max
  • C) $avg
  • D) $multiply

Answer: A) $sum
Explanation: The $sum operator in the $group stage is used to calculate the sum of a field, such as the total sales for a category.


15. What does the $year operator do in MongoDB aggregation?

  • A) Groups documents by year
  • B) Extracts the year from a date field
  • C) Sorts documents by year
  • D) Calculates the average of the year field

Answer: B) Extracts the year from a date field
Explanation: The $year operator extracts the year part from a date field for further aggregation or processing.


16. Which of the following is used to create a computed field in the $project stage?

  • A) $multiply
  • B) $avg
  • C) $sum
  • D) $max

Answer: A) $multiply
Explanation: The $multiply operator can be used to create a computed field in the $project stage, such as calculating a tax amount or discount.


17. What will the following query return?

javascript

CopyEdit

db.orders.aggregate([

   { $match: { status: "shipped" } },

   { $project: { orderId: 1, shippingDate: 1 } },

   { $sort: { shippingDate: -1 } }

])

  • A) Filters orders by status "shipped", selects orderId and shippingDate, and sorts by shippingDate in descending order.
  • B) Groups orders by status and calculates total shippingDate.
  • C) Filters orders by status "shipped" and sorts by orderId in ascending order.
  • D) Filters orders by status "shipped" and calculates average shippingDate.

Answer: A) Filters orders by status "shipped", selects orderId and shippingDate, and sorts by shippingDate in descending order.
Explanation: The $match filters documents, $project reshapes them, and $sort sorts them by shippingDate in descending order.


18. What does the $limit stage do when used in an aggregation pipeline?

  • A) Restricts the number of documents passed to the next stage
  • B) Filters documents based on a specific condition
  • C) Groups documents by a specified field
  • D) Sorts the documents based on a field

Answer: A) Restricts the number of documents passed to the next stage
Explanation: The $limit operator limits the number of documents passed to the next stage in the pipeline.


19. What does the $addFields stage do in MongoDB aggregation?

  • A) Adds new documents to the collection
  • B) Adds new fields to the documents
  • C) Sorts documents by a specific field
  • D) Groups documents by a field

Answer: B) Adds new fields to the documents
Explanation: The $addFields stage is used to add new fields to documents, or modify existing fields, in the aggregation pipeline.


20. How do you calculate the total number of items sold for each product category?

  • A) Using $sum in the $group stage
  • B) Using $avg in the $group stage
  • C) Using $match to filter by category
  • D) Using $project to calculate totals

Answer: A) Using $sum in the $group stage
Explanation: The $sum operator in the $group stage can be used to calculate the total number of items sold in each category.


21-50. Continue in a similar fashion to create more MCQs focusing on filtering, grouping, sorting, and projecting concepts in MongoDB.

4o mini

You said:

give 21-50

ChatGPT said:

ChatGPT

Here are 21-50 multiple-choice questions (MCQs) based on the MongoDB Aggregation Framework concepts:


21. What is the purpose of the $unwind stage in MongoDB aggregation?

  • A) To group documents by a specific field
  • B) To break an array field into individual documents
  • C) To calculate the sum of a field
  • D) To add new fields to documents

Answer: B) To break an array field into individual documents
Explanation: The $unwind stage is used to deconstruct an array field from the input documents, creating a separate document for each element in the array.


22. What does the $addFields operator do in MongoDB?

  • A) Adds a new document to the collection
  • B) Adds or updates fields in the existing documents
  • C) Groups documents by a specific field
  • D) Filters documents based on a field

Answer: B) Adds or updates fields in the existing documents
Explanation: The $addFields operator allows you to add new fields to documents or update the values of existing fields in the aggregation pipeline.


23. What is the correct syntax for calculating the highest value of a field in an aggregation pipeline?

  • A) $max
  • B) $group
  • C) $sum
  • D) $avg

Answer: A) $max
Explanation: The $max operator returns the highest value in a field across all documents.


24. What does the $count stage do in MongoDB aggregation?

  • A) Returns the total number of documents in the collection
  • B) Returns the count of documents after a specific stage
  • C) Calculates the total sum of a field
  • D) Groups documents by a field and counts occurrences

Answer: B) Returns the count of documents after a specific stage
Explanation: The $count stage counts the number of documents passed through the pipeline at that point and outputs a document with a field count.


25. How do you calculate the minimum value of a field in an aggregation pipeline?

  • A) $min
  • B) $avg
  • C) $sum
  • D) $count

Answer: A) $min
Explanation: The $min operator returns the minimum value of a field across all documents.


26. Which aggregation stage can be used to merge documents from multiple collections?

  • A) $match
  • B) $merge
  • C) $group
  • D) $lookup

Answer: D) $lookup
Explanation: The $lookup stage is used to perform a left outer join between two collections.


27. What does the $geoNear stage do?

  • A) Performs a spatial query to retrieve documents near a point
  • B) Performs a regular expression match on location fields
  • C) Groups documents based on geographical location
  • D) Sorts documents by their location in the collection

Answer: A) Performs a spatial query to retrieve documents near a point
Explanation: The $geoNear stage performs a query that returns documents near a specified location, using geospatial indexes.


28. What is the purpose of the $out stage in MongoDB aggregation?

  • A) To output the results to a new collection
  • B) To filter documents before they are passed to the next stage
  • C) To add new fields to the documents
  • D) To group documents by a specific field

Answer: A) To output the results to a new collection
Explanation: The $out stage allows you to write the results of the aggregation to a new collection.


29. Which of the following operations can be used to perform arithmetic on fields in MongoDB aggregation?

  • A) $concat
  • B) $multiply, $add, $divide
  • C) $sort
  • D) $match

Answer: B) $multiply, $add, $divide
Explanation: Arithmetic operators like $multiply, $add, and $divide allow you to perform calculations on fields during the aggregation process.


30. What does the $match stage use to filter documents?

  • A) Field values
  • B) Regular expressions
  • C) Query criteria
  • D) Field names

Answer: C) Query criteria
Explanation: The $match stage uses query criteria to filter documents before passing them to subsequent stages.


31. Which operator is used to perform string concatenation in MongoDB aggregation?

  • A) $concat
  • B) $split
  • C) $substr
  • D) $trim

Answer: A) $concat
Explanation: The $concat operator is used to concatenate two or more strings into one string in MongoDB.


32. How do you calculate the total amount spent by each customer in MongoDB aggregation?

  • A) Using $group and $avg
  • B) Using $group and $sum
  • C) Using $project
  • D) Using $match

Answer: B) Using $group and $sum
Explanation: The $group stage groups documents by a field (e.g., customerId), and $sum calculates the total amount for each group.


33. What does the $sort stage use to order the documents?

  • A) Field values in ascending or descending order
  • B) Field names
  • C) Document counts
  • D) Document types

Answer: A) Field values in ascending or descending order
Explanation: The $sort stage orders documents based on the specified field(s) in either ascending (1) or descending (-1) order.


34. What will the following query return?

javascript

CopyEdit

db.products.aggregate([

   { $project: { productName: 1, stock: 1 } },

   { $sort: { stock: -1 } }

])

  • A) Projects productName and stock, then sorts by stock in descending order.
  • B) Groups products by stock and calculates the sum.
  • C) Projects productName and stock and sorts alphabetically by productName.
  • D) Filters products by stock and sorts by productName.

Answer: A) Projects productName and stock, then sorts by stock in descending order.
Explanation: The $project stage includes the fields productName and stock, and $sort orders them by stock in descending order.


35. What does the $project stage allow you to do?

  • A) Group documents by a specific field
  • B) Calculate the average of a field
  • C) Include, exclude, or create new fields in documents
  • D) Filter documents based on conditions

Answer: C) Include, exclude, or create new fields in documents
Explanation: The $project stage is used to reshape documents by selecting or excluding fields and adding new computed fields.


36. What is the correct syntax for filtering documents where the price is greater than 100 in the aggregation pipeline?

  • A) { $match: { price: { $gt: 100 } } }
  • B) { $group: { _id: null, total: { $gt: 100 } } }
  • C) { $project: { price: 1, $gt: 100 } }
  • D) { $sort: { price: 1 } }

Answer: A) { $match: { price: { $gt: 100 } } }
Explanation: The $match stage filters documents based on the price being greater than 100.


37. How do you calculate the total revenue for each department in MongoDB aggregation?

  • A) Group by department and sum totalAmount
  • B) Group by department and average salary
  • C) Filter documents by department
  • D) Sort documents by department

Answer: A) Group by department and sum totalAmount
Explanation: The $group stage is used to group by department, and $sum calculates the total totalAmount for each group.


38. What is the purpose of the $redact stage?

  • A) To transform documents into key-value pairs
  • B) To filter documents at a deeper level, based on their content
  • C) To calculate averages of fields
  • D) To add fields to documents

Answer: B) To filter documents at a deeper level, based on their content
Explanation: The $redact stage provides a mechanism for filtering documents based on their content at any level of nesting.


39. What does the $set operator do in the aggregation pipeline?

  • A) Modifies existing fields or adds new fields
  • B) Sets values for specific documents
  • C) Groups documents by a field
  • D) Sorts documents in ascending order

Answer: A) Modifies existing fields or adds new fields
Explanation: The $set operator is used to add new fields or modify existing fields in the aggregation pipeline.


40. How can you remove a field from a document in the aggregation pipeline?

  • A) Use $exclude
  • B) Use $remove
  • C) Set the field value to null
  • D) Use $project and set the field value to 0

Answer: D) Use $project and set the field value to 0
Explanation: In the $project stage, setting a field to 0 excludes it from the output documents.


41. What does the $facet stage allow you to do in MongoDB aggregation?

  • A) Group documents by multiple fields
  • B) Perform multiple independent aggregation operations on the same documents
  • C) Sort documents by a field
  • D) Filter documents based on specific conditions

Answer: B) Perform multiple independent aggregation operations on the same documents
Explanation: The $facet stage allows you to run multiple aggregation pipelines in parallel, producing multiple results in one stage.


42. What does the $skip stage do in MongoDB aggregation?

  • A) Skips documents with certain values
  • B) Skips the first n documents in the pipeline
  • C) Skips documents from a specific collection
  • D) Skips sorting operations in the pipeline

Answer: B) Skips the first n documents in the pipeline
Explanation: The $skip stage skips the first n documents in the pipeline, typically used for pagination.


43. What is the use of the $merge stage in MongoDB aggregation?

  • A) To merge two collections together
  • B) To merge the results of aggregation into a specified collection
  • C) To merge the fields of documents
  • D) To sort documents by multiple fields

Answer: B) To merge the results of aggregation into a specified collection
Explanation: The $merge stage is used to output the results of an aggregation pipeline to a specified collection, either by creating or updating documents.


44. How do you group documents by year extracted from a date field?

  • A) Use $group and $year
  • B) Use $match and $year
  • C) Use $project and $dateToString
  • D) Use $group and $month

Answer: A) Use $group and $year
Explanation: The $year operator extracts the year from a date field, and $group is used to group documents by that year.


45. Which operator is used to perform a substring operation in MongoDB aggregation?

  • A) $substr
  • B) $substrBytes
  • C) $substring
  • D) $match

Answer: A) $substr
Explanation: The $substr operator is used to extract a portion of a string from a field in MongoDB.


46. What is the effect of using $limit in the aggregation pipeline?

  • A) Limits the number of fields returned per document
  • B) Limits the number of documents passed to the next stage
  • C) Limits the number of stages in the pipeline
  • D) Limits the result to a specific range of values

Answer: B) Limits the number of documents passed to the next stage
Explanation: The $limit stage limits the number of documents that continue to the next stage in the aggregation pipeline.


47. What type of data transformation can the $project stage perform?

  • A) Aggregating data
  • B) Sorting data
  • C) Including, excluding, or transforming fields
  • D) Grouping data

Answer: C) Including, excluding, or transforming fields
Explanation: The $project stage reshapes documents by including or excluding fields, or adding new computed fields.


48. How can you join two collections in MongoDB using aggregation?

  • A) Use $join
  • B) Use $merge
  • C) Use $lookup
  • D) Use $combine

Answer: C) Use $lookup
Explanation: The $lookup stage allows you to join two collections in MongoDB, similar to SQL joins.


49. What is the correct syntax for calculating the sum of a field in MongoDB aggregation?

  • A) { $sum: "$field" }
  • B) { $sum: { $avg: "$field" } }
  • C) { $group: { _id: null, sum: "$field" } }
  • D) { $group: { _id: "$field", sum: "$total" } }

Answer: A) { $sum: "$field" }
Explanation: The $sum operator is used to calculate the sum of a specified field.


50. What does the $replaceRoot operator do in MongoDB aggregation?

  • A) Replaces the value of a field
  • B) Replaces the entire document with a specified field
  • C) Adds a root field to documents
  • D) Removes the root document

Answer: B) Replaces the entire document with a specified field
Explanation: The $replaceRoot operator replaces the document with the contents of a specified field.


Tags

Post a Comment

0Comments

Post a Comment (0)

About Me