Final Exam Question 2

“Pipeline 3 is correct and will execute with no error”.

I can easily determine whether Pipeline 3 will “execute with no error”. However, how can I determine whether Pipeline 3 is “correct” without knowing what Pipeline 3 is supposed to do???

Hi,

You should pay attention to the expressions and operators to determine if they are used correctly.

José Carlos

Again, in order to determine whether the expressions and operators are “used correctly”, I need to know what they are supposed to be doing. For example, is Pipeline 3 supposed to find the largest element of the array, “a”, or is it supposed to calculate the sum of the elements in array, “a”, or is it supposed to be doing something else? Depending on what the pipeline is supposed to do it may be regarded as correct or incorrect even though it executes with no error.

Hi,

If you think the projection is correct and the use of the $$ROOT variable is correct, then the pipeline is OK. Anyway, the pipeline 3 is supposed to project the maximum value of the “a” array, then group by “$$ROOT._id” to sum up all the projected “a” values by group (which actually does nothing, as there is only one maximum per document). So if we had three documents, where the maximum values in the “a” array are 7, 313, and 34, the output should be something like this:

{ "_id" : ObjectId("5bba319f45479cff24cffea9"), "all_as" : 7 }
{ "_id" : ObjectId("5bba319a45479cff24cffea8"), "all_as" : 313 }
{ "_id" : ObjectId("5bba315245479cff24cffea7"), "all_as" : 34 }

José Carlos