[SERVER-33808] $and Query and "Implicit $and" queries are not consistent Created: 12/Mar/18  Updated: 27/Oct/23  Resolved: 12/Mar/18

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 3.6.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Vincenzo [X] Assignee: Ramon Fernandez Marina
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File Screen Shot 2018-03-08 at 5.23.43 PM.png     PNG File Screen Shot 2018-03-08 at 5.24.54 PM.png    
Operating System: ALL
Participants:

 Description   

I found a very strange behaviour with mongodb 3.6.3, running on Linux Centos 6.0

I always thought that those two queries:

{
   "a": 1,
   "b": 2
}

and

{
   $and:[
   {"a":1},
   {"b":2}
]
}

were equivalent, but with the latest version (3.6.3) the results are completely different!

Look at the two screenshots attached, the underlying collection is the same but the only correct result is the one with "$and" clause. In the example "births" can be a String or a Number.

I ran the same query also within the mongoshell with the same inconsistent bug.

Please fix it as soon as possible.



 Comments   
Comment by Ramon Fernandez Marina [ 12/Mar/18 ]

Glad to hear this one is sorted out, closing the ticket.

Regards,
Ramón.

Comment by Vincenzo [X] [ 12/Mar/18 ]

Great! You are right:

mongodev:PRIMARY> let x = {"births" : {$type : 2}, "births": {$ne: "NA"}}
mongodev:PRIMARY> x
{ "births" : { "$ne" : "NA" } }
mongodev:PRIMARY> 

At least now I know I must focus on how I build queries with multiple conditions on the same field.

Comment by Ramon Fernandez Marina [ 12/Mar/18 ]

The queries you're issuing are not of the shape {"a": 1, "b": 2}, but {"a":1, "a":2}.

I'm not familiar with the program you're using to query the database, but when using the shell repeated fields are ignored, so it may be the same case in your program and what you're querying is either {"births" : {$type : 2}} or {"births": {$ne: "NA"}. Please check the program's documentation, but it's likely that

{"births" : {$type : 2}, "births": {$ne: "NA"}}

is being converted to

{"births": {$ne: "NA"}}

and that's why you need to use $and.

Regards,
Ramón.

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