Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 1
Suppose you have a companies collection in your database. Only the following documents are stored in this collection:
- {
- _id: ObjectId("52cdef7c4bab8bd675297da4"),
- name: 'Powerset',
- category_code: 'search',
- founded_year: 2006
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297da5"),
- name: 'Technorati',
- category_code: 'advertising',
- founded_year: 2002
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297da7"),
- name: 'AddThis',
- category_code: 'advertising',
- founded_year: 2004
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297da8"),
- name: 'OpenX',
- category_code: 'advertising',
- founded_year: 2008
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297daa"),
- name: 'Sparter',
- category_code: 'games_video',
- founded_year: 2007
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297dac"),
- name: 'Veoh',
- category_code: 'games_video',
- founded_year: 2004
- },
- {
- _id: ObjectId("52cdef7c4bab8bd675297dae"),
- name: 'Thoof',
- category_code: 'web',
- founded_year: 2006
- }
How many documents will be returned in response to the following aggregation pipeline?
- [{ $group: {
- _id: '$category_code',
- number_of_companies: {
- $sum: 1
- }
- }}, { $sort: {
- number_of_companies: -1
- }}, { $limit: 3 }]
-
A
2
-
B
3
-
C
0
-
D
4
Reveal correct answer
Correct answer: B
A.
The pipeline is designed to return the top 3 groups, not just 2. Even if two groups had the same number of companies, the $limit stage would still return 3 documents, so the correct answer is 3, not 2.
B.
The $group stage groups documents by category_code, resulting in four groups: 'search', 'advertising', 'games_video', and 'web'. The $sort stage then orders these groups by number_of_companies in descending order. Finally, the $limit stage restricts the output to the top 3 groups, meaning 3 documents are returned.
C.
The aggregation pipeline does produce results. Specifically, it returns the top 3 groups based on the number of companies, so the correct answer is not 0.
D.
Although there are four unique category_code values, the $limit stage ensures that only the top 3 groups are returned, not 4.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
