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

Errors::DocumentNotFound.new(klass, ids, ids -…

Error message

Errors::DocumentNotFound.new(klass, ids, ids - result.map(&:_id))

What it means

check_for_missing_documents! compares the executed query result count against the requested ids: fewer documents came back and Mongoid.raise_not_found_error is on, so DocumentNotFound is raised listing the missing ids.

Solutions

  1. Ensure all requested ids exist before querying
  2. Set Mongoid.raise_not_found_error = false to get partial results instead of raising
  3. Rescue Mongoid::Errors::DocumentNotFound and handle missing ids
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at lib/mongoid/criteria.rb:582 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/b81135e3ab114e83. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/criteria.rb:582

    private

    # Are documents in the query missing, and are we configured to raise an
    # error?
    #
    # @api private
    #
    # @example Check for missing documents.
    #   criteria.check_for_missing_documents!([], [ 1 ])
    #
    # @param [ Array<Document> ] result The result.
    # @param [ Array<Object> ] ids The ids.
    #
    # @raise [ Errors::DocumentNotFound ] If none are found and raising an
    #   error.
    def check_for_missing_documents!(result, ids)
      return unless (result.size < ids.size) && Mongoid.raise_not_found_error

      raise Errors::DocumentNotFound.new(klass, ids, ids - result.map(&:_id))
    end

    # Clone or dup the current +Criteria+. This will return a new criteria with
    # the selector, options, klass, embedded options, etc intact.
    #
    # @api private
    #
    # @example Clone a criteria.
    #   criteria.clone
    #
    # @example Dup a criteria.
    #   criteria.dup
    #
    # @param [ Criteria ] other The criteria getting cloned.
    #
    # @return [ nil ] nil.
    def initialize_copy(other)
      @inclusions = other.inclusions.dup

View on GitHub (pinned to 0da0e23d71)