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

$expr operators are not implicitly ANDed in compound queries

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      If I move both $expr condition into one $expr with an explicit $and, the query performs correctly, I don't understand why the implicit AND doesn't work in this case.

      Show
      If I move both $expr condition into one $expr with an explicit $and, the query performs correctly, I don't understand why the implicit AND doesn't work in this case.

      Description

      The documentation for queries states that,

      Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

      Therefore, I expect this query with two $expr fields to find documents with an array where field foo is the same at indexes 0 and 1, and field bar is different:

      db.collection.find({
        $expr: {
          $eq: [
            { $arrayElemAt: ['$arrO.foo', 0] },
            { $arrayElemAt: ['$arrO.foo', 1] }
          ]
        },
        $expr: {
          $ne: [
            { $arrayElemAt: ['$arrO.bar', 0] },
            { $arrayElemAt: ['$arrO.bar', 1] }
          ]
        }
      });
      

      What happens is that other documents are returned as well, where foo is different between arrO[0] and arrO[1].

      Collection:

      [
        {
          arrO: [
            {
              foo: 1,
              bar: "bar diff..."
            },
            {
              foo: 2,
              bar: "bar ...erent",
              
            }
          ]
        },
        {
          arrO: [
            {
              foo: 3,
              bar: "bar diff..."
            },
            {
              foo: 3,
              bar: "bar ...erent"
            }
          ]
        }
      ]
      

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: