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

Could not find a document of class

Error message

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

What it means

Raised by the private raise_document_not_found_error in Mongoid::Contextual::Memory when an in-memory (embedded/enumerable) context operation such as find or take! finds no matching document of the given class. This is a sentinel-style guard converting an empty result into Mongoid::Errors::DocumentNotFound.

Solutions

  1. Verify the id(s) exist among the loaded documents
  2. Load the documents from the database or rescue Mongoid::Errors::DocumentNotFound
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at lib/mongoid/contextual/memory.rb:784 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/06236591886c4db9. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/contextual/memory.rb:784

      # Pluck the field values from the given document.
      #
      # @param [ Document ] doc The document to pluck from.
      # @param [ [ String | Symbol ]... ] *fields Field(s) to pluck.
      #
      # @return [ Object | Array<Object> ] The plucked values.
      def pluck_from_doc(doc, *fields)
        if fields.length == 1
          retrieve_value_at_path(doc, fields.first)
        else
          fields.map do |field|
            retrieve_value_at_path(doc, field)
          end
        end
      end

      def raise_document_not_found_error
        raise Errors::DocumentNotFound.new(klass, nil, nil)
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)