[SERVER-23262] Using a $elemMatch with two $eq queries returns incorrect results Created: 21/Mar/16  Updated: 23/Mar/16  Resolved: 23/Mar/16

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

Type: Bug Priority: Major - P3
Reporter: Jason Mccullough Assignee: Kelsey Schubert
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2008R2, MongoChef


Operating System: ALL
Steps To Reproduce:

doc:

{ "STags" : [ "OD:52_domain.com", "OL:92_mlbx" ] }
{ "STags" : [ "OD:49_domain.com", "OL:92_mlbx" ] }

Queries:

db.test.find({"STags" : { $elemMatch : { $eq : "OD:52_domain.com", $eq : "OL:92_mlbx"} }})
db.test.find({"STags" : { $elemMatch : { $eq : "OD:sdsfsfd", $eq : "OL:92_mlbx"} }})

Both queries return two documents. even though 2nd query specifies a values that does NOT exist in any document.

Replacing the $elemMath by a $and does not exhibit the same behaviour and returns the correct results.

Participants:

 Description   

Using a $elemMatch with two $eq queries is returning all documents that only one of the $eq. And which one is matched depends on the order of the $eq in the statement.



 Comments   
Comment by Kelsey Schubert [ 23/Mar/16 ]

Hi gnarlyrides@gmail.com,

Thank you for the report. This is expected behavior, and is a result of how javascript objects/dicts work:

> x = {a:2, a:1}
{a:1}

You cannot have repeated fields of the same name at the same level.

My understanding is that you have identified that a query using the $and operator fulfills the needs of your use case.

Kind regards,
Thomas

Comment by Jason Mccullough [ 21/Mar/16 ]

Explain gives:
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"STags" : {
"$elemMatch" : {
"" :

{ "$eq" : "OL:92_mlbx" }

}
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]

clearly considering only one eq...

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