{"record":{"id":"5e63453391fda864","repo":"symfony/http-foundation","slug":"you-must-provide-the-database-and-collection-option-for","errorCode":null,"errorMessage":"You must provide the \"database\" and \"collection\" option for MongoDBSessionHandler.","messagePattern":"You must provide the \"database\" and \"collection\" option for MongoDBSessionHandler\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Session/Storage/Handler/MongoDbSessionHandler.php","lineNumber":70,"sourceCode":"     * A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions\n     * automatically. Such an index can for example look like this:\n     *\n     *     db.<session-collection>.createIndex(\n     *         { \"<expiry-field>\": 1 },\n     *         { \"expireAfterSeconds\": 0 }\n     *     )\n     *\n     * More details on: https://docs.mongodb.org/manual/tutorial/expire-data/\n     *\n     * If you use such an index, you can drop `gc_probability` to 0 since\n     * no garbage-collection is required.\n     *\n     * @throws \\InvalidArgumentException When \"database\" or \"collection\" not provided\n     */\n    public function __construct(Client|Manager $mongo, array $options)\n    {\n        if (!isset($options['database']) || !isset($options['collection'])) {\n            throw new \\InvalidArgumentException('You must provide the \"database\" and \"collection\" option for MongoDBSessionHandler.');\n        }\n\n        if ($mongo instanceof Client) {\n            $mongo = $mongo->getManager();\n        }\n\n        $this->manager = $mongo;\n        $this->namespace = $options['database'].'.'.$options['collection'];\n\n        $this->options = array_merge([\n            'id_field' => '_id',\n            'data_field' => 'data',\n            'time_field' => 'time',\n            'expiry_field' => 'expires_at',\n        ], $options);\n        $this->ttl = $this->options['ttl'] ?? null;\n    }\n","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/Session/Storage/Handler/MongoDbSessionHandler.php#L52-L88","documentation":"MongoDbSessionHandler requires an explicit MongoDB database and collection name to know where sessions are stored. The constructor throws an InvalidArgumentException if either the 'database' or 'collection' key is missing from the $options array.","triggerScenarios":"Instantiating MongoDbSessionHandler with an options array that omits 'database' or 'collection', e.g. new MongoDbSessionHandler($client, []) or passing only connection settings.","commonSituations":"Framework config (e.g. Symfony session handler services) missing the mongo db/collection keys; copying a DSN-only setup without handler options; renaming config keys during a refactor.","solutions":["Add both 'database' and 'collection' keys to the $options array passed to the constructor.","Verify the service/config definition that builds MongoDbSessionHandler passes the full options map (e.g. ['database' => 'app', 'collection' => 'sessions']).","Check that env-driven config variables for database/collection are actually set in the current environment."],"exampleFix":"// before\n$handler = new MongoDbSessionHandler($client, ['database' => 'app']);\n\n// after\n$handler = new MongoDbSessionHandler($client, ['database' => 'app', 'collection' => 'sessions']);","handlingStrategy":"validation","validationCode":"if (empty($options['database']) || empty($options['collection'])) {\n    throw new \\InvalidArgumentException('database and collection are required for MongoDbSessionHandler');\n}\n$handler = new MongoDbSessionHandler($client, $options);","typeGuard":null,"tryCatchPattern":"try {\n    $handler = new MongoDbSessionHandler($client, $options);\n} catch (\\InvalidArgumentException $e) {\n    // fix config and retry / fail fast with a clear config error\n}","preventionTips":["Centralize session handler options in one validated config object.","Assert presence of 'database' and 'collection' in config loading tests.","Keep env vars for db/collection documented and checked at boot."],"tags":["mongodb","sessions","configuration","invalid-argument"],"backgroundTag":"missing-required-option","analyzedSha":"5aea19cd678fa4140f6108406f1096de5e9ed6e4","analyzedAt":"2026-09-13T01:52:22.855Z","contentChangedAt":"2026-09-13T01:52:22.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}