[SERVER-67027] Applying more than one search predicate on a Multi Key Indexed field. Created: 05/Jun/22  Updated: 27/Oct/23  Resolved: 08/Jun/22

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

Type: Bug Priority: Minor - P4
Reporter: Yomi SEa Assignee: Backlog - Triage Team
Resolution: Works as Designed Votes: 0
Labels: query-parser
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Server Triage
Operating System: ALL
Steps To Reproduce:

Sample Data:

db.getCollection('foo').insertOne({
    "docArray" : [ {"code" : 10}, {"code" : 20}],
});

 

Query:  constantly false condition, a number can't be less than and greater than itself.

db.getCollection('foo').find({
    "docArray.code" : { "$gt" : 13,  "$lt":13 }
}); 

 

Expected Result: 

No Result

Actual Result :

Returns the inserted document

 

Fixed Query:  a sample query that looks like has the expected behavior.

db.getCollection('foo').find({
    "docArray" : { 
        "$elemMatch" : {"code" : { "$gt" : 13,  "$lt":13 }} 
    }
});

 

Participants:

 Description   

Querying on an array of nested documents will split each search condition and apply them separately to the documents in the array.

 



 Comments   
Comment by Eric Sedor [ 08/Jun/22 ]

Hi p6tachi@gmail.com,

This is as expected based on the behavior of multikey index bounds. When you want to apply multiple criteria to a single element of an array, you must use $elemMatch.

If you have additional questions about this behavior or other questions about the queries for your use-case, we recommend starting with the MongoDB Developer Community Forums.

Thank you!

Generated at Thu Feb 08 06:07:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.