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
    • Needed
    • Hide

      Drivers must document restrictions for filters in the countDocuments API that result from the API using aggregation pipeline.

      Suggested wording:

      Due to countDocuments using the $match aggregation pipeline stage, certain query operators cannot be used in countDocuments. This includes the $where and $near query operators, among others. Details can be found in the documentation for the $match aggregation pipeline stage.

      The documentation link for $match is https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/#restrictions.

      Show
      Drivers must document restrictions for filters in the countDocuments API that result from the API using aggregation pipeline. Suggested wording: Due to countDocuments using the $match aggregation pipeline stage, certain query operators cannot be used in countDocuments. This includes the $where and $near query operators, among others. Details can be found in the documentation for the $match aggregation pipeline stage. The documentation link for $match is https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/#restrictions .
    • $i18n.getText("admin.common.words.hide")
      Key Status/Resolution FixVersion
      CDRIVER-5783 Backlog
      CXX-3148 Backlog
      CSHARP-5387 Backlog
      GODRIVER-3406 Backlog
      JAVA-5678 Backlog
      NODE-6483 Needs Triage
      MOTOR-1398 Duplicate
      PYTHON-4932 Works as Designed
      PHPLIB-1575 Works as Designed
      RUBY-3578 Backlog
      RUST-2081 Backlog
      $i18n.getText("admin.common.words.show")
      #scriptField, #scriptField *{ border: 1px solid black; } #scriptField{ border-collapse: collapse; } #scriptField td { text-align: center; /* Center-align text in table cells */ } #scriptField td.key { text-align: left; /* Left-align text in the Key column */ } #scriptField a { text-decoration: none; /* Remove underlines from links */ border: none; /* Remove border from links */ } /* Add green background color to cells with FixVersion */ #scriptField td.hasFixVersion { background-color: #00FF00; /* Green color code */ } /* Center-align the first row headers */ #scriptField th { text-align: center; } Key Status/Resolution FixVersion CDRIVER-5783 Backlog CXX-3148 Backlog CSHARP-5387 Backlog GODRIVER-3406 Backlog JAVA-5678 Backlog NODE-6483 Needs Triage MOTOR-1398 Duplicate PYTHON-4932 Works as Designed PHPLIB-1575 Works as Designed RUBY-3578 Backlog RUST-2081 Backlog

      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.

            Assignee:
            andreas.braun@mongodb.com Andreas Braun
            Reporter:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Andreas Braun Andreas Braun
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: