Operational Defect Database

BugZero found this defect 59 days ago.

MongoDB | 2616423

Fix incorrect result of unoptimized pipeline with $match and $count

Last update date:

3/21/2024

Affected products:

MongoDB Server

Affected releases:

No affected releases provided.

Fixed releases:

No fixed releases provided.

Description:

Info

The following aggregation pipeline produces incorrect result when run with disabled optimization: db.xxx.aggregate([ {$match: {a: {$ne: 2}}}, {$match: {b: {$exists: false}}}, {"$count" : "a"} ]); The incorrect result is observed both with classic and sbe engines. If we remove the $count stage, the pipeline produces correct result. The pipeline works correctly with optimization and with a collection scan if the index does not exist. The execution stats on the second $match expression `{$match: {b: {$exists: false}}}` shows 1 returned result in the pipeline without $count, and 0 nReturned in the other case. See the full explain in the attachments. // no $count: {"$match" : { "b" : { "$not" : { "$exists" : true } } }, "nReturned" : NumberLong(1), "executionTimeMillisEstimate" : NumberLong(1) }, ... // with $count {"$match" : { "b" : { "$not" : { "$exists" : true } } }, "nReturned" : NumberLong(0), "executionTimeMillisEstimate" : NumberLong(1) }, ...

Top User Comments


Steps to Reproduce

db.xxx.insertMany([ {_id: 1, a: 1, b: null, c: 3}, {_id: 2, a: 2, b: null, c: 4}, {_id: 3, a: 2, b: 2, c: 3}, {_id: 4, a: 2, c: 5}, {_id: 5, a: 3, b: null, c: 6}, {_id: 6, a: 4,  c: 7}]); db.xxx.createIndex({a:1, b:1}); db.adminCommand({configureFailPoint: 'disablePipelineOptimization', mode: 'alwaysOn'}); db.xxx.aggregate([ {$match: {a: {$ne: 2}}}, {$match: {b: {$exists: false}}}, {"$count" : "a"} ]);

Additional Resources / Links

Share:

BugZero® Risk Score

What's this?

Coming soon

Status

Needs Scheduling

Learn More

Search:

...