Summary
Using the $where operator in countDocument filter is not supported.
As specified and implemented in drivers, the countDocuments operation uses an aggregation pipeline and copies the value of the filter argument into a $match stage.
If the filter contains a $where operator, a server error is returned.
MongoServerError: $where is not allowed in this context
This is an issue because the signature of find(filter, options) and countDocuments(filter, options) are the same. So users expect to be able to use the same filter for both methods.
Related discussion: https://github.com/mongodb/laravel-mongodb/discussions/2625
Motivation
Who is the affected end user?
Application developers
How does this affect the end user?
Yes, blocked.
How likely is it that this problem or use case will occur?
Everytime a developer tries to countDocuments on a find that they wrote using $where operator.
If the problem does occur, what are the consequences and how severe are they?
The command doesn't work. Developers must write their own aggregation pipeline or find a way to not use $where in the filter.
Is this issue urgent?
No
Is this ticket required by a downstream team?
No
Is this ticket only for tests?
No
Acceptance Criteria
Provide an helpful error message or update the generated aggregation pipeline so that $where can be used in countDocuments.
- is caused by
-
DRIVERS-501 Implement new count API
- Closed
-
PHPORM-50 Remove usage of the deprecated MongoDB\Collection::count
- Closed