C100DBA practice question 24 of 25
Consider that you have a collection called population which has fields state and city. Which of the following query will calculate the population grouped by state and city?
Full set: 130 questionsThis query correctly uses the aggregation framework's $group stage to meet the requirements. It groups documents by a compound id key composed of the state and city fields. The $sum accumulator is then used with the field path "$pop" to calculate the total of the pop field for all documents within each group. This provides the total population for each unique state and city combination. (Note: The provided option contains typos, but its structure is the only one that correctly implements the required logic).
A. This option has multiple syntax errors and uses $sum: 1, which counts documents rather than summing the values in the pop field.
B. This option has syntax errors and uses $pop, which is an update operator and not a valid accumulator for the $group stage.
D. This query incorrectly groups only by city, failing to meet the requirement of grouping by both state and city.
1. MongoDB Manual, $group (aggregation): "Groups input documents by the specified id expression and for each distinct grouping, outputs a document." (https://www.mongodb.com/docs/manual/reference/operator/aggregation/group/)
2. MongoDB Manual, $sum (aggregation): "Calculates and returns the collective sum of numeric values. $sum ignores non-numeric values." (https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum/)
3. MongoDB University, M121: The MongoDB Aggregation Framework, Chapter 2, "Basic Aggregation - $group and $match". This courseware covers the fundamental usage of $group with a compound id and accumulators like $sum.
Keep practicing
Take the free C100DBA practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other C100DBA practice questions
- Question 1If the value of totalKeysExamined is 30000 and the value of totalDocsExamined is 0, which…
- Question 2The oplog (operations log) is a special capped collection that keeps a rolling record of …
- Question 3‘$set' sets the value of
- Question 4What does the totalKeysExamined field returned by the explain method indicate?
- Question 5Which mongodb tools allow us to work with our data in a human readable format?
- Question 6Which of the following is incorrect statement about find and findOne operations in MongoD…
