Final Exam: Answer to question 5

The proposed solution doesn’t include the field “min”. Therefore, it cannot be considered the right answer, since it is not going to produce the expected results

Original pipeline:

var pipeline = [
… {"$addFields": { “min”: {"$min": “$sunnydays”}}},
… {"$addFields": { “mean”: {"$avg": “$sunnydays” }}},
… {"$sort": {“city”: 1}},
… {"$match": { “country”: “USA”, “min”: {"$gte": 200}, “mean”: {"$gte": 220}}},
… ]

db.cities.aggregate(pipeline)
{ “_id” : 10, “city” : “San Diego”, “region” : “CA”, “country” : “USA”, “sunnydays” : [ 220, 232, 205, 211, 242, 270 ], “min” : 205, “mean” : 230 }

Proposed Solution:
var pipeline = [
… {"$match": { “country”: “USA”}},
… {"$addFields": { “mean”: {"$avg": “$sunnydays”}}},
… {"$match": { “mean”: {"$gte": 220}, “sunnydays”: {"$not": {"$lt": 200 }}}},
… {"$sort": {“city”: 1}},
… ]

db.cities.aggregate(pipeline)
{ “_id” : 10, “city” : “San Diego”, “region” : “CA”, “country” : “USA”, “sunnydays” : [ 220, 232, 205, 211, 242, 270 ], “mean” : 230 }

Hi @beatrizia,

I see your point, but after reviewing the question there’s no mention about the document shape. It looks like the solution indeed solves the problem more efficiently.

I’ll ask the team to edit the question to explicitly include that the document shape may be different.

José Carlos

1 Like

Beatrizia,

thanks for the suggestion.

Daniel.