|
The optimization of $exists is only partially addressed by SERVER-28889. It works in case the field in the partial filter expression is not indexed. For instance:
db.coll.createIndex({a: 1}, {name: "a_1_b_exists", "partialFilterExpression": {b: {$exists: true}}});
|
db.coll.find({a: {$gte: 90}, b: {$exists: true}}, {_id: 0, a: 1});
|
In this case, the $exists predicate is removed and the fetch stage is not needed.
If the field is also indexed, the $exists predicate is tagged to be satisfied by the index. Since null and missing values are not distinguished in the index, the 'tightness' of the index scan is determined as INEXACT_FETCH, which leads to keeping the predicate in the filter and adding a FETCH stage to the plan.
db.coll.createIndex({a: 1, b:1}, {name: "a_1_b_1_b_exists", "partialFilterExpression": {b: {$exists: true}}});
|
db.coll.find({a: {$gte: 90}, b: {$exists: true}}, {_id: 0, a: 1});
|
A possible solution is to have a special treatment for $exists predicate during index analysis and give a priority to the index filter expression over index tagging. This is not covered by SERVER-28889.
|