[SERVER-13664] Query with $elemMatch using a compound multikey index can generate incorrect results Created: 19/Apr/14  Updated: 11/Jul/16  Resolved: 21/Apr/14

Status: Closed
Project: Core Server
Component/s: Index Maintenance, Querying
Affects Version/s: 2.6.0
Fix Version/s: 2.6.1, 2.7.0

Type: Bug Priority: Major - P3
Reporter: Ben Rotz Assignee: David Storch
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Backport Completed:
Steps To Reproduce:

db.test.drop();
db.test.insert({ "__e" : [ { "t" : 1, "n" : 25 }, { "t" : 3, "n" : 59 } ] });
db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2, $lt : 4}, 'n' : 25}}});
#no record returned as expected
 
db.test.ensureIndex({'__e.t' : 1, '__e.n' : 1});
db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2, $lt : 4}, 'n' : 25}}});
#record returned (BUG)

Participants:

 Description   
Issue Status as of April 21, 2014

ISSUE SUMMARY
When using $elemMatch on a compound multikey index, the result set is not always filtered correctly and may in certain edge cases return additional results that do not match the query. See the comments in this ticket for more details and an example.

USER IMPACT
Users affected by this bug may get query results that do not match the query predicate.

WORKAROUNDS
None.

RESOLUTION
The patch fixes a logical bug where $elemMatch expressions were not correctly handled. Specifically, the result set needs to be filtered to verify that the document correctly matches all predicates of the $elemMatch document.

AFFECTED VERSIONS
MongoDB version 2.6.0 is affected by this bug.

PATCHES
The patch is included in the 2.6.1 production release.

Original description

Looks like in 2.6 there is a bug when a complex index exists and you use a range query in the first part of the index.

I have tested this on 2.6.0 and 2.4.4. The bug does not exist on 2.4.4



 Comments   
Comment by Githook User [ 21/Apr/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13664 never detach filters from inside elemMatch object
(cherry picked from commit 15bd7092e059a66ee74b65da9186c29300b70b2a)
Branch: v2.6
https://github.com/mongodb/mongo/commit/e17c3e6dc06362c268836f2d3289c42e14b81e04

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13664 never detach filters from inside elemMatch object
Branch: master
https://github.com/mongodb/mongo/commit/15bd7092e059a66ee74b65da9186c29300b70b2a

Comment by Ben Rotz [ 19/Apr/14 ]

Also can be noted that the bug doesn't exist if the range query only has one bound:

#record should not return in following query; works as expected
db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2}, 'n' : 25}}});
 
#record should not return in following query; does not work as expected
db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2, $lt : 4}, 'n' : 25}}});

Comment by Ben Rotz [ 19/Apr/14 ]

It should be noted that the bug doesn't exist if the index is not complex:

#if only this index is on the collection, no bug
db.test.ensureIndex({'__e.t' : 1});

Comment by Ben Rotz [ 19/Apr/14 ]

I started this issue at

https://groups.google.com/forum/#!topic/mongodb-user/4lcEJ111dmk

But all the info to reproduce should be in this ticket

Generated at Thu Feb 08 03:32:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.