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

Could not find a document of class %{klass}.

Error message

Could not find a document of class %{klass}.

What it means

Error "Could not find a document of class %{klass}." thrown in mongodb/mongoid.

Source

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

          # Do to_a first so that the Mongo#first method is not used and the
          # result is not sorted.
          limit(1).to_a.first
        end
      end

      # Take one document from the database and raise an error if there are none.
      #
      # @example Take a document
      #   context.take!
      #
      # @return [ Document ] The document.
      #
      # @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no
      #   documents to take.
      def take!
        # Do to_a first so that the Mongo#first method is not used and the
        # result is not sorted.
        raise Errors::DocumentNotFound.new(klass, nil, nil) unless fst = limit(1).to_a.first

        fst
      end

      # Get a hash of counts for the values of a single field. For example,
      # if the following documents were in the database:
      #
      #   { _id: 1, age: 21 }
      #   { _id: 2, age: 21 }
      #   { _id: 3, age: 22 }
      #
      #   Model.tally("age")
      #
      # would yield the following result:
      #
      #   { 21 => 2, 22 => 1 }
      #
      # When tallying a field inside an array or embeds_many association:

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/contextual/mongo.rb:384 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/8c3c5eb174d7236f. Report an issue: GitHub.