[SERVER-42018] $expr operators are not implicitly ANDed in compound queries Created: 30/Jun/19  Updated: 20/Aug/20  Resolved: 16/Jul/19

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Dan Dascalescu Assignee: Eric Sedor
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-6439 Duplicate fields at the same level sh... Backlog
Operating System: ALL
Steps To Reproduce:

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.

Participants:

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



 Comments   
Comment by Eric Sedor [ 16/Jul/19 ]

I am going to close this ticket as a duplicate of SERVER-6439. You can watch that ticket for updates on this behavior. Thanks again!

Comment by Eric Sedor [ 03/Jul/19 ]

dandv, actually--independent of version--this is likely due to the duplicate $expr fields at the same level of the query document. SERVER-6439 is open about this but in short, an explicit $and is required when specifying statements like this.

Comment by Eric Sedor [ 02/Jul/19 ]

Hi dandv; thanks for your report so far. Can you please clarify what version you are seeing this on?

Generated at Thu Feb 08 04:59:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.