Uploaded image for project: 'Drivers'
  1. Drivers
  2. DRIVERS-2728

Document that countDocuments() uses $match and may not support the same filters as find/count commands

    • Type: Icon: Spec Change Spec Change
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Component/s: CRUD
    • Labels:
    • Needed


      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


      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?


      Is this ticket required by a downstream team?


      Is this ticket only for tests?


      Acceptance Criteria

      Provide an helpful error message or update the generated aggregation pipeline so that $where can be used in countDocuments.

            Unassigned Unassigned
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            0 Vote for this issue
            3 Start watching this issue