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

Incorrect result when contained $or rewrites $elemMatch extra condition

    • Query Optimization
    • Minor Change
    • ALL
    • v7.0, v6.0, v5.0, v4.4
    • QO 2023-05-15, QO 2023-05-29, QO 2023-06-12, QO 2023-06-26, QO 2023-07-10

      Sample data:

      [
        { c: 1, p: [ { id: 1 } ] },
        { c: 2, p: [ { id: 1 } ] },
        { c: 3, p: [ { id: 1 } ] },
        { c: 4, p: [ { id: 1 } ] }
      ]
      

      Index must be present to trigger the bug:

      { "p.id": 1, c: 1}
      

      Query in question:

      db.test.find(
      {$and:[
            {$or:[{c:{$lt:2}},{c:{$gt:3}}]},
            {p:{$elemMatch:{id:{$eq:1,$exists:true}}}}
      ]}
      )
      

      Without the index, it matches two documents. With index but without (redundant) $exists:true inside $elemMatch it also returns two documents. But with index and with redundant exists it fails to return any documents.

            Assignee:
            svilen.mihaylov@mongodb.com Svilen Mihaylov (Inactive)
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: