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
- split to
-
CDRIVER-5783 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
CSHARP-5387 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
CXX-3148 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
GODRIVER-3406 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
JAVA-5678 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
RUBY-3578 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
RUST-2081 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Backlog
-
MOTOR-1398 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Closed
-
NODE-6483 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Closed
-
PHPLIB-1575 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Closed
-
PYTHON-4932 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Closed