mongodb/mongoid · error · Mongoid::Errors::InvalidEstimatedCountScoping

Invalid criteria for estimated_count.

Error message

Invalid criteria for estimated_count.

What it means

Error "Invalid criteria for estimated_count." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/contextual/mongo.rb:98

          view.count(options)
        end
      end

      # Get the estimated number of documents matching the query.
      #
      # Unlike count, estimated_count does not take a block because it is not
      # traditionally defined (with a block) on Enumerable like count is.
      #
      # @example Get the estimated number of matching documents.
      #   context.estimated_count
      #
      # @param [ Hash ] options The options, such as maxTimeMS to be factored
      #   into the count.
      #
      # @return [ Integer ] The number of matches.
      def estimated_count(options = {})
        unless criteria.selector.empty?
          raise Mongoid::Errors::InvalidEstimatedCountScoping.new(klass) if klass.default_scoping?

          raise Mongoid::Errors::InvalidEstimatedCountCriteria.new(klass)

        end
        view.estimated_document_count(options)
      end

      # Delete all documents in the database that match the selector.
      #
      # @example Delete all the documents.
      #   context.delete
      #
      # @return [ nil ] Nil.
      def delete
        view.delete_many.deleted_count
      end
      alias delete_all delete

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/contextual/mongo.rb:98 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/0a02dd63432ebb92. Report an issue: GitHub.