> ts.find()
{ "time" : ISODate("2023-07-19T20:37:09.316Z"), "meta" : 1, "val" : 5, "_id" : 0 }
{ "time" : ISODate("2023-07-19T20:37:31.758Z"), "meta" : 1, "val" : 21, "_id" : 1 }
// filter by the date of the document with _id: 1
> ts.aggregate([{$match: {time: ISODate("2023-07-19T20:37:31.758Z")}}])
{ "time" : ISODate("2023-07-19T20:37:31.758Z"), "meta" : 1, "val1" : 3, "val" : 21, "_id" : 1 }
// add group on the metaField after the filter
> ts.aggregate([{$match: {time: ISODate("2023-07-19T20:37:31.758Z")}},{$group: {_id: "$meta", m: {$min: "$val"}}}])
{ "_id" : 1, "m" : 5 }
The result of the last query is incorrect – it should have been {_id: 1, m: 21}.
The problem doesn't repro if group key isn't the metaField and does repro if filter on any non-metaField.
The problem is with the $group rewrite that replaces "$group": {"_id": "$meta", "m": {"$min": "$val2" }} with "$group": {"_id": "$meta", "m": {"$min": "$control.min.val2"}} without taking in account that the minimum should be computed over a subset of the bucket.