{"record":{"id":"8c3c5eb174d7236f","repo":"mongodb/mongoid","slug":"could-not-find-a-document-of-class-klass-8c3c5e","errorCode":null,"errorMessage":"Could not find a document of class %{klass}.","messagePattern":"Could not find a document of class %(.+?)\\.","errorType":"exception","errorClass":"Mongoid::Errors::DocumentNotFound","httpStatus":404,"severity":"error","filePath":"lib/mongoid/contextual/mongo.rb","lineNumber":384,"sourceCode":"          # Do to_a first so that the Mongo#first method is not used and the\n          # result is not sorted.\n          limit(1).to_a.first\n        end\n      end\n\n      # Take one document from the database and raise an error if there are none.\n      #\n      # @example Take a document\n      #   context.take!\n      #\n      # @return [ Document ] The document.\n      #\n      # @raise [ Mongoid::Errors::DocumentNotFound ] raises when there are no\n      #   documents to take.\n      def take!\n        # Do to_a first so that the Mongo#first method is not used and the\n        # result is not sorted.\n        raise Errors::DocumentNotFound.new(klass, nil, nil) unless fst = limit(1).to_a.first\n\n        fst\n      end\n\n      # Get a hash of counts for the values of a single field. For example,\n      # if the following documents were in the database:\n      #\n      #   { _id: 1, age: 21 }\n      #   { _id: 2, age: 21 }\n      #   { _id: 3, age: 22 }\n      #\n      #   Model.tally(\"age\")\n      #\n      # would yield the following result:\n      #\n      #   { 21 => 2, 22 => 1 }\n      #\n      # When tallying a field inside an array or embeds_many association:","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/contextual/mongo.rb#L366-L402","documentation":"Error \"Could not find a document of class %{klass}.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/contextual/mongo.rb:384 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}