{"record":{"id":"009b50bb11f9282e","repo":"mongodb/mongoid","slug":"errors-documentnotfound-new-klass-nil-nil","errorCode":null,"errorMessage":"Errors::DocumentNotFound.new(klass, nil, nil)","messagePattern":"Errors::DocumentNotFound\\.new\\(klass, nil, nil\\)","errorType":"exception","errorClass":"Mongoid::Errors::DocumentNotFound","httpStatus":404,"severity":"error","filePath":"lib/mongoid/contextual/none.rb","lineNumber":327,"sourceCode":"\n      # Always returns zero.\n      #\n      # @example Get the length of null context.\n      #   context.length\n      #\n      # @return [ Integer ] Always zero.\n      def length\n        entries.length\n      end\n      alias size length\n\n      alias find_first first\n      alias one first\n\n      private\n\n      def raise_document_not_found_error\n        raise Errors::DocumentNotFound.new(klass, nil, nil)\n      end\n    end\n  end\nend\n","sourceCodeStart":309,"sourceCodeEnd":332,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/contextual/none.rb#L309-L332","documentation":"Raised by the private raise_document_not_found_error in Mongoid::Contextual::None. The None context is returned for criteria that can never match (e.g. Mongoid.none / Model.none), so any find/first-style call that requires a document always raises DocumentNotFound with nil id/ids — a sentinel error representing guaranteed emptiness.","triggerScenarios":"Thrown at lib/mongoid/contextual/none.rb:327 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check criteria.exists?/any? before bang queries on possibly-null criteria","Rescue Mongoid::Errors::DocumentNotFound for optional-document flows"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","contentChangedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}