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 }