{"record":{"id":"bd41360813ff356e","repo":"mongodb/mongoid","slug":"mongoid-model-has-shard-config-but-is-not-pers","errorCode":null,"errorMessage":"MONGOID: #{model} has shard config but is not persisted in a sharded cluster: #{model.collection.cluster.summary}","messagePattern":"MONGOID: #(.+?) has shard config but is not persisted in a sharded cluster: #(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/mongoid/tasks/database.rb","lineNumber":184,"sourceCode":"      # Returns the model classes that have had their collections sharded,\n      # including model classes whose collections had already been sharded\n      # prior to the invocation of this method.\n      #\n      # @example Shard all collections\n      #   Mongoid::Tasks::Database.shard_collections\n      #\n      # @return [ Array<Class> ] The sharded models\n      def shard_collections(models = ::Mongoid.models)\n        models.map do |model|\n          next if model.shard_config.nil?\n\n          if model.embedded? && !model.cyclic?\n            logger.warn(\"MONGOID: #{model} has shard config but is embedded\")\n            next\n          end\n\n          unless model.collection.cluster.sharded? || model.collection.cluster.load_balanced?\n            logger.warn(\"MONGOID: #{model} has shard config but is not persisted in a sharded cluster: #{model.collection.cluster.summary}\")\n            next\n          end\n\n          # Database of the collection must exist in order to run collStats.\n          # Depending on server version, the collection itself must also\n          # exist.\n          # MongoDB does not have a command to create the database; the best\n          # approximation of it is to create the collection we want.\n          # On older servers, creating a collection that already exists is\n          # an error.\n          # Additionally, 3.6 and potentially older servers do not provide\n          # the error code when they are asked to collStats a non-existent\n          # collection (https://jira.mongodb.org/browse/SERVER-50070).\n          begin\n            model.collection.database.command(collStats: model.collection.name).first\n          rescue Mongo::Error::OperationFailure => e\n            # Code 26 is database does not exist.\n            # Code 8 is collection does not exist, as of 4.0.","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/tasks/database.rb#L166-L202","documentation":"Error \"MONGOID: #{model} has shard config but is not persisted in a sharded cluster: #{model.collection.cluster.summary}\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/tasks/database.rb:184 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"}