Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-79136

Incorrect query result of $match + $group on metaField over time-series

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 7.1.0-rc0, 5.0.20, 6.0.9, 7.0.0-rc9
    • Affects Version/s: 6.0.8, 7.0.0-rc8
    • Component/s: None
    • None
    • Fully Compatible
    • ALL
    • v7.0, v5.0
    • QI 2023-07-24, QI 2023-08-07

      > 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.

            Assignee:
            irina.yatsenko@mongodb.com Irina Yatsenko (Inactive)
            Reporter:
            irina.yatsenko@mongodb.com Irina Yatsenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: