MCQs Of Day 10: Aggregation Framework – Part 1 (Basics)

Rashmi Mishra
0

 MCQs Of Day 10: Aggregation Framework – Part 1 (Basics)

1. What is the purpose of the aggregation framework in MongoDB?

  • A) To filter documents
  • B) To perform complex data transformations and calculations
  • C) To index data
  • D) To store data efficiently

Answer: B) To perform complex data transformations and calculations
Explanation: The aggregation framework allows you to transform and process data in complex ways, such as grouping, filtering, summing, and averaging, making it useful for data analysis.


2. Which of the following MongoDB operators is used to group documents?

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

Answer: B) $group
Explanation: The $group operator is used to group documents by a specified field and apply aggregation functions like sum, average, etc.


3. What is the default sorting order when using the $sort operator?

  • A) Ascending
  • B) Descending
  • C) No sorting
  • D) Alphabetical

Answer: A) Ascending
Explanation: By default, $sort sorts in ascending order. You can specify the order by passing 1 for ascending or -1 for descending.


4. Which operator in MongoDB's aggregation framework calculates the average of a field?

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

Answer: C) $avg
Explanation: The $avg operator calculates the average of a specified field across documents in a group.


5. What does the $match operator do in MongoDB aggregation?

  • A) Groups documents by a field
  • B) Filters documents based on a condition
  • C) Sorts documents
  • D) Projects specified fields

Answer: B) Filters documents based on a condition
Explanation: The $match operator filters documents by a specified condition, similar to the find() query in MongoDB.


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

  • A) Sorts documents
  • B) Groups documents
  • C) Reshapes documents by including or excluding fields
  • D) Filters documents

Answer: C) Reshapes documents by including or excluding fields
Explanation: The $project stage allows you to reshape the documents by adding, removing, or renaming fields.


7. Which operator is used to find the total sum of a specific field across documents?

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

Answer: A) $sum
Explanation: The $sum operator calculates the total sum of a specified field.


8. Which of the following stages is used to limit the number of documents returned in an aggregation query?

  • A) $group
  • B) $limit
  • C) $match
  • D) $skip

Answer: B) $limit
Explanation: The $limit stage restricts the number of documents returned by the aggregation query.


9. What is the result of using $count in the aggregation pipeline?

  • A) It counts the number of documents in a collection
  • B) It counts the number of documents per group
  • C) It returns the total sum of a field
  • D) It groups documents based on a field

Answer: B) It counts the number of documents per group
Explanation: The $count operator counts the number of documents in each group during aggregation.


10. What does the $addFields operator do?

  • A) It adds new fields to documents
  • B) It removes fields from documents
  • C) It sorts documents
  • D) It groups documents

Answer: A) It adds new fields to documents
Explanation: The $addFields operator adds new fields or modifies existing fields in the documents.


11. How do you sort documents by a field in descending order?

  • A) { $sort: { field: 1 } }
  • B) { $sort: { field: -1 } }
  • C) { $sort: { field: 0 } }
  • D) { $sort: { field: true } }

Answer: B) { $sort: { field: -1 } }
Explanation: To sort documents in descending order, you use -1 for the field in the $sort stage.


12. Which aggregation operator is used to select only the required fields from documents?

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

Answer: A) $project
Explanation: The $project operator is used to select specific fields to include or exclude from the output.


13. What does the $unwind operator do in MongoDB?

  • A) Flattens an array field into separate documents
  • B) Groups documents by a field
  • C) Sorts documents
  • D) Filters documents by a condition

Answer: A) Flattens an array field into separate documents
Explanation: The $unwind operator takes an array field and creates separate documents for each element in the array.


14. Which of the following aggregation operators is used to get the minimum value of a field?

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

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


15. What does the $skip operator do in an aggregation pipeline?

  • A) Skips the first n documents in the pipeline
  • B) Skips the last n documents in the pipeline
  • C) Filters out documents
  • D) Groups documents

Answer: A) Skips the first n documents in the pipeline
Explanation: The $skip operator skips the first n documents in the pipeline, based on the number provided.


16. Which of the following is NOT an aggregation operator?

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

Answer: C) $unique
Explanation: MongoDB does not have an aggregation operator called $unique. Operators like $sum, $count, and $avg are part of the aggregation framework.


17. What is the use of the $lookup operator in MongoDB?

  • A) To group documents by a specific field
  • B) To join documents from two collections
  • C) To filter documents
  • D) To sort documents

Answer: B) To join documents from two collections
Explanation: The $lookup operator is used to join data from two collections, similar to a SQL JOIN.


18. How do you perform an aggregation on a field in MongoDB?

  • A) Use the find() method
  • B) Use the aggregate() method
  • C) Use the update() method
  • D) Use the insert() method

Answer: B) Use the aggregate() method
Explanation: The aggregate() method is used to perform complex aggregations on data in MongoDB.


19. What is the result of using the $first operator in a $group stage?

  • A) It returns the first document in a group
  • B) It returns the last document in a group
  • C) It returns the maximum value in a group
  • D) It returns the sum of all values in a group

Answer: A) It returns the first document in a group
Explanation: The $first operator returns the first value from the group of documents being processed.


20. Which of the following is a valid use of the $match stage in the aggregation pipeline?

  • A) Grouping data
  • B) Filtering documents
  • C) Sorting documents
  • D) Projecting fields

Answer: B) Filtering documents
Explanation: The $match stage is used to filter documents based on a condition.


21. Which operator in MongoDB can calculate the highest value in a field?

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

Answer: A) $max
Explanation: The $max operator calculates the highest value for a specified field across all documents.


22. What is the purpose of the $addToSet operator in MongoDB aggregation?

  • A) To add elements to an array field, ensuring uniqueness
  • B) To calculate the sum of an array
  • C) To flatten an array field
  • D) To add documents to a group

Answer: A) To add elements to an array field, ensuring uniqueness
Explanation: The $addToSet operator adds values to an array but only includes unique values, removing duplicates.


23. What does the $merge operator do in MongoDB aggregation?

  • A) Merges multiple collections into one
  • B) Combines the results of an aggregation into a new collection
  • C) Merges data from an external file
  • D) Merges fields into a single field

Answer: B) Combines the results of an aggregation into a new collection
Explanation: The $merge operator allows you to save the results of an aggregation query into a new collection.


24. Which of the following is an example of an aggregation stage in MongoDB?

  • A) $merge
  • B) $join
  • C) $delete
  • D) $distinct

Answer: A) $merge
Explanation: $merge is a valid aggregation stage that allows you to save the aggregation results to a collection.


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

  • A) To output the results of the aggregation to an external file
  • B) To store the results of an aggregation into a new collection
  • C) To sort documents
  • D) To match documents

Answer: B) To store the results of an aggregation into a new collection
Explanation: The $out stage outputs the results of an aggregation query to a specified collection.


I will stop here for now to keep it manageable, but these 25 questions should give a solid foundation in MongoDB aggregation concepts. Would you like me to continue with more MCQs?

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are additional 25 MCQs based on the Aggregation Framework in MongoDB, continuing from the previous set:


26. What is the use of the $bucket operator in MongoDB aggregation?

  • A) To categorize data into predefined ranges
  • B) To group data by a field
  • C) To filter out certain documents
  • D) To project specific fields

Answer: A) To categorize data into predefined ranges
Explanation: The $bucket operator allows you to group documents into discrete ranges (buckets), which can be useful for histogram-like operations.


27. Which of the following operators is used to find the unique values of a field in MongoDB aggregation?

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

Answer: A) $addToSet
Explanation: The $addToSet operator returns an array of unique values from a specified field, similar to a DISTINCT operation.


28. What is the purpose of the $match stage in the aggregation pipeline?

  • A) To remove unwanted fields from the documents
  • B) To combine multiple collections
  • C) To filter documents before processing them further
  • D) To calculate totals and averages

Answer: C) To filter documents before processing them further
Explanation: The $match stage is used to filter documents, so only relevant documents are passed to the subsequent stages.


29. What does the $project stage do in an aggregation pipeline?

  • A) Filters documents
  • B) Groups documents by a field
  • C) Reshapes each document by including, excluding, or adding fields
  • D) Sorts documents

Answer: C) Reshapes each document by including, excluding, or adding fields
Explanation: The $project stage reshapes the documents by modifying which fields are included or excluded in the result set.


30. What is the purpose of the $addFields stage in the aggregation pipeline?

  • A) To perform mathematical calculations
  • B) To add or modify fields in documents
  • C) To sort documents by a field
  • D) To group documents by a field

Answer: B) To add or modify fields in documents
Explanation: The $addFields stage allows you to add new fields or modify existing fields in each document.


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

  • A) Limits the number of fields in each document
  • B) Limits the number of documents returned
  • C) Groups documents by a field
  • D) Sorts documents

Answer: B) Limits the number of documents returned
Explanation: The $limit stage restricts the number of documents returned by the aggregation query to a specified number.


32. What operator would you use in MongoDB aggregation to compute the standard deviation of a field?

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

Answer: A) $stdDevPop
Explanation: The $stdDevPop operator calculates the standard deviation of a given numeric field, providing a measure of how spread out the values are.


33. Which operator is used in MongoDB's aggregation pipeline to perform a left outer join between two collections?

  • A) $lookup
  • B) $unionWith
  • C) $join
  • D) $merge

Answer: A) $lookup
Explanation: The $lookup operator allows you to join data from two collections, similar to a left outer join in SQL.


34. What does the $geoNear operator in MongoDB aggregation do?

  • A) Performs geographic data queries
  • B) Calculates the total distance between two points
  • C) Filters documents based on geographic location
  • D) Groups geographic data by region

Answer: A) Performs geographic data queries
Explanation: The $geoNear operator is used to find documents that are near a given point, and it is typically used for geographical queries with 2dsphere indexes.


35. Which of the following is used to calculate the total count of documents in the aggregation pipeline?

  • A) $count
  • B) $group
  • C) $sum
  • D) $total

Answer: A) $count
Explanation: The $count operator counts the number of documents that pass through the aggregation pipeline.


36. What is the role of the $facet operator in MongoDB aggregation?

  • A) To join multiple collections
  • B) To define multiple aggregation pipelines within a single query
  • C) To group documents by a specific field
  • D) To calculate averages

Answer: B) To define multiple aggregation pipelines within a single query
Explanation: The $facet operator allows you to execute multiple aggregation pipelines in parallel within a single query.


37. What is the use of the $sortByCount operator in MongoDB aggregation?

  • A) Sorts documents by a specific field
  • B) Groups documents by a field and sorts by count
  • C) Counts the number of documents in a collection
  • D) Performs a join between two collections

Answer: B) Groups documents by a field and sorts by count
Explanation: The $sortByCount operator groups documents by a specified field and then sorts the groups by their count.


38. What is the function of the $redact stage in the aggregation pipeline?

  • A) To filter documents based on values
  • B) To modify document fields
  • C) To restrict access to fields in documents based on conditions
  • D) To calculate values for a new field

Answer: C) To restrict access to fields in documents based on conditions
Explanation: The $redact operator is used to control document access based on conditions, allowing or restricting fields.


39. What does the $sample stage do in an aggregation pipeline?

  • A) Samples a specified number of documents randomly
  • B) Groups documents into random sets
  • C) Filters documents randomly
  • D) Projects specific fields randomly

Answer: A) Samples a specified number of documents randomly
Explanation: The $sample operator randomly selects a specified number of documents from a collection.


40. What does the $skip operator do in the aggregation pipeline?

  • A) Skips a specified number of documents at the start
  • B) Skips a specified number of documents at the end
  • C) Filters documents based on criteria
  • D) Groups documents by a field

Answer: A) Skips a specified number of documents at the start
Explanation: The $skip operator skips the first n documents in the pipeline based on the number provided.


41. Which of the following aggregation stages can be used to exclude documents from the pipeline?

  • A) $exclude
  • B) $remove
  • C) $match
  • D) $filter

Answer: C) $match
Explanation: The $match stage is used to filter documents and exclude those that don't meet the specified condition.


42. In MongoDB aggregation, what is the purpose of the $group operator?

  • A) To combine the values of a specified field for all documents
  • B) To combine fields into an array
  • C) To filter documents based on a condition
  • D) To calculate the count of documents

Answer: A) To combine the values of a specified field for all documents
Explanation: The $group operator is used to group documents by a field and apply aggregation functions like sum, avg, and count.


43. Which MongoDB aggregation operator is used to calculate the average value of a specific field in a group?

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

Answer: B) $avg
Explanation: The $avg operator is used to calculate the average value of a specified field across all documents in the group.


44. Which stage in the aggregation pipeline is used to join data from two collections?

  • A) $unionWith
  • B) $lookup
  • C) $merge
  • D) $join

Answer: B) $lookup
Explanation: The $lookup stage is used to join data from another collection based on a specified condition.


45. How do you calculate the total salary of employees by department using MongoDB aggregation?

  • A) Use the $sum operator with the $group stage
  • B) Use the $avg operator with the $group stage
  • C) Use the $count operator
  • D) Use the $project stage

Answer: A) Use the $sum operator with the $group stage
Explanation: The $sum operator is used to calculate the total salary of employees within each department in the $group stage.


46. Which operator is used in MongoDB aggregation to add a new field to each document?

  • A) $addFields
  • B) $sum
  • C) $count
  • D) $project

Answer: A) $addFields
Explanation: The $addFields stage is used to add new fields to documents or modify existing ones during aggregation.


47. What type of join does $lookup perform in MongoDB aggregation?

  • A) Inner join
  • B) Left outer join
  • C) Right outer join
  • D) Full outer join

Answer: B) Left outer join
Explanation: The $lookup operator performs a left outer join, meaning it returns all documents from the left collection and matches documents from the right collection based on a specified condition.


48. What is the effect of using the $unwind stage in MongoDB aggregation?

  • A) Flattens an array into separate documents
  • B) Groups documents by a field
  • C) Filters documents based on a condition
  • D) Sorts documents

Answer: A) Flattens an array into separate documents
Explanation: The $unwind operator is used to deconstruct an array field and output a separate document for each element in the array.


49. Which of the following aggregation stages is used to remove a specified field from documents?

  • A) $remove
  • B) $exclude
  • C) $project
  • D) $unwind

Answer: C) $project
Explanation: The $project stage allows you to include or exclude specific fields from documents in the aggregation result.


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

  • A) Run multiple aggregation pipelines in parallel
  • B) Perform a left join between two collections
  • C) Flatten an array field
  • D) Filter documents based on a condition

Answer: A) Run multiple aggregation pipelines in parallel
Explanation: The $facet stage enables you to execute multiple aggregation pipelines concurrently, and the results can be combined into one document.


 


Tags

Post a Comment

0Comments

Post a Comment (0)

About Me