Final: Question 2

The collection defined in this question is as follows:

“a”: [1, 34, 13]

Question 1 : Can you explain, how did you manage to get a collection without the _id field? The _id field should have been created by default, and since you haven’t used a projection to unset it, how come your output above is not printing _id field?

Question 2: Following aggregation pipeline has been provided.

Pipeline 1
{"$match": { “a” : {"$sum": 1} }},
{"$project": { “_id” : {"$addToSet": “$a”} }},
{"$group": { “_id” : “”, “max_a”: {"max" : "_id"} }}

Here stage 2 is having a $addToSet, so my understanding is that it should transform the _id field into an array, by pushing in the value of $a (i.e an array with only 1 element). Moving to the next stage, i.e. Stage 3 - The $group will pick all documents from the collection and group them, since it is using a null string for grouping _id. It is using an accumulator of $max, on _id field from stage 2. Since _id from Stage 2 was transformed into an array filed, how does $max even work with arrays, finding a max array across a group of arrays?

Are there any takers for my questions? MongoDB TA team?


This is a theory and use question and so your Question 1 is irrelevant – the collection is as given; how it might be generated is not an issue.

If you now consider the actual question in the Exam, then an analysis of the pipeline stages shows you which of the given statements are correct or incorrect. Notice that some of the pipelines are designed to be incorrect; your issue is to figure out which ones are correct or incorrect and why, Therefore you should expect that some of the stages in the pipelines are, in fact, incorrect and therefore will NOT work. So your Question 2 is also irrelevant, as you are asking how one of the pipelines provided could possibly work – consider that perhaps it doesn’t.