When a single condition is specified on a filter, the $and
operator is still used.
As a result, a condition on a unique index like _id
equality is not using an index lookup plan but rather might use an index scan.
A test that shows this behavior is this one.
I believe the fix can be rather easy - here when the formattedConditions
is a single array element we could do:
this.formatAggregation = async (aggregator, formatedConditions) => {
+ if (formatedConditions.length === 1) return formatedConditions[0];
const aggregatorOperator = this.formatAggregatorOperator(aggregator);
return { [aggregatorOperator]: formatedConditions };
};
Instead of today:
this.formatAggregation = async (aggregator, formatedConditions) => {
const aggregatorOperator = this.formatAggregatorOperator(aggregator);
return { [aggregatorOperator]: formatedConditions };
};
Expected behavior
This could be a limitation of the mongodb planner - but in reality - the query generator from Liana could decide to not use $and
and $or
(condition aggregator operator) when a single filter condition is requested.
Actual behavior
$and
is used even on a single condition filter.
Context
- Package Version: “forest-express-mongoose”: “^6.1.0”
- Database Version: Mongodb 3.6 (pretty sure this applies to 4 as well)