{"record":{"id":"40d12ccfe54919dd","repo":"quarkusio/quarkus","slug":"illegal-read-preference-configured-in-the-mongoe","errorCode":null,"errorMessage":"Illegal read preference  configured in the @MongoEntity annotation of .. Supported values are primary|primaryPreferred|secondary|secondaryPreferred|nearest","messagePattern":"Illegal read preference  configured in the @MongoEntity annotation of \\.\\. Supported values are primary\\|primaryPreferred\\|secondary\\|secondaryPreferred\\|nearest","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/ReactiveMongoOperations.java","lineNumber":230,"sourceCode":"\n        if (Panache.getCurrentSession() != null) {\n            return collection.deleteOne(Panache.getCurrentSession(), query).onItem().ignore().andContinueWithNull();\n        }\n        return collection.deleteOne(query).onItem().ignore().andContinueWithNull();\n    }\n\n    public ReactiveMongoCollection mongoCollection(Class<?> entityClass) {\n        MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);\n        ReactiveMongoDatabase database = mongoDatabase(mongoEntity);\n        if (mongoEntity != null) {\n            ReactiveMongoCollection collection = mongoEntity.collection().isEmpty()\n                    ? database.getCollection(entityClass.getSimpleName(), entityClass)\n                    : database.getCollection(mongoEntity.collection(), entityClass);\n            if (!mongoEntity.readPreference().isEmpty()) {\n                try {\n                    collection = collection.withReadPreference(ReadPreference.valueOf(mongoEntity.readPreference()));\n                } catch (IllegalArgumentException iae) {\n                    throw new IllegalArgumentException(\"Illegal read preference \" + mongoEntity.readPreference()\n                            + \" configured in the @MongoEntity annotation of \" + entityClass.getName() + \".\"\n                            + \" Supported values are primary|primaryPreferred|secondary|secondaryPreferred|nearest\");\n                }\n            }\n            return collection;\n        }\n        return database.getCollection(entityClass.getSimpleName(), entityClass);\n    }\n\n    public ReactiveMongoDatabase mongoDatabase(Class<?> entityClass) {\n        MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class);\n        return mongoDatabase(mongoEntity);\n    }\n\n    //\n    // Private stuff\n\n    public Uni<Void> nullUni() {","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/reactive/runtime/ReactiveMongoOperations.java#L212-L248","documentation":"mongoCollection() applies the @MongoEntity annotation's readPreference by calling ReadPreference.valueOf(); if that value is not one of the MongoDB driver's known names, the driver throws IllegalArgumentException, which Panache re-throws with a message listing the supported values: primary, primaryPreferred, secondary, secondaryPreferred, nearest.","triggerScenarios":"Setting @MongoEntity(readPreference = \"...\") to a misspelled or unsupported value (e.g. 'Primary', 'primary ', 'majority', 'secondary-preferred') on an entity class, then running any query/update that resolves the collection.","commonSituations":"Typos or wrong casing in the annotation; using dash-instead-of-camelCase ('secondary-preferred'); copying readPreference strings from MongoDB connection-string options that use different naming; misremembering 'nearest' as 'closest'.","solutions":["Set readPreference to exactly one of: primary, primaryPreferred, secondary, secondaryPreferred, nearest (case is handled by valueOf but spelling must match)","Verify the annotation on the entity class referenced in the error message (entityClass.getName())","If you need driver-specific tags/read-preference behavior, configure it on the MongoClient instead of the annotation"],"exampleFix":"// before\n@MongoEntity(collection = \"orders\", readPreference = \"secondary-preferred\")\n\n// after\n@MongoEntity(collection = \"orders\", readPreference = \"secondaryPreferred\")","handlingStrategy":"validation","validationCode":"private static final Set<String> VALID_RP = Set.of(\"primary\",\"primaryPreferred\",\"secondary\",\"secondaryPreferred\",\"nearest\");\nString rp = entityAnnotation.readPreference();\nif (!rp.isEmpty() && !VALID_RP.contains(rp)) {\n    throw new IllegalArgumentException(\"Bad readPreference: \" + rp);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep readPreference strings in a constants class to avoid typos","Add a startup-time unit test that resolves the collection for every @MongoEntity","Copy values verbatim from the driver docs, not from connection-string option names"],"tags":["panache","mongodb","configuration","annotation","read-preference"],"backgroundTag":"invalid-config-value","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}