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

$expr operators are not implicitly ANDed in compound queries

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • ALL
    • 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.

      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"
            }
          ]
        }
      ]
      

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            dandv Dan Dascalescu
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: