{"id":"2df07b3b111892eb","repo":"mongodb/node-mongodb-native","slug":"database-names-cannot-contain-the-character","errorCode":null,"errorMessage":"Database names cannot contain the character '.'","messagePattern":"Database names cannot contain the character '\\.'","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/db.ts","lineNumber":159,"sourceCode":"\n  /**\n   * Creates a new Db instance.\n   *\n   * Db name cannot contain a dot, the server may apply more restrictions when an operation is run.\n   *\n   * @param client - The MongoClient for the database.\n   * @param databaseName - The name of the database this instance represents.\n   * @param options - Optional settings for Db construction.\n   */\n  constructor(client: MongoClient, databaseName: string, options?: DbOptions) {\n    options = options ?? {};\n\n    // Filter the options\n    options = filterOptions(options, DB_OPTIONS_ALLOW_LIST);\n\n    // Ensure there are no dots in database name\n    if (typeof databaseName === 'string' && databaseName.includes('.')) {\n      throw new MongoInvalidArgumentError(`Database names cannot contain the character '.'`);\n    }\n\n    // Internal state of the db object\n    this.s = {\n      // Options\n      options,\n      // Unpack read preference\n      readPreference: ReadPreference.fromOptions(options),\n      // Merge bson options\n      bsonOptions: resolveBSONOptions(options, client),\n      // Set up the primary key factory or fallback to ObjectId\n      pkFactory: options?.pkFactory ?? DEFAULT_PK_FACTORY,\n      // ReadConcern\n      readConcern: ReadConcern.fromOptions(options),\n      writeConcern: WriteConcern.fromOptions(options),\n      // Namespace\n      namespace: new MongoDBNamespace(databaseName)\n    };","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/db.ts#L141-L177","documentation":"The Db constructor validates that the database name does not contain a dot because dots are MongoDB namespace separators (db.collection). A dot in a db name would corrupt namespace parsing, routing, and command construction.","triggerScenarios":"client.db('my.app'), new Db(client, 'tenant.prod'), or any path that constructs a Db with a dotted name.","commonSituations":"Multi-tenant names using dots; passing a full 'db.collection' namespace to client.db(); mis-decoded connection-string dbName.","solutions":["Replace dots in the name with dashes or underscores (e.g. 'tenant_app')","If the dot was meant to separate db from collection, pass only the db portion to client.db()","Sanitize external/user input before passing it to client.db()"],"exampleFix":"// before\nconst db = client.db('tenant.app');\n// after\nconst db = client.db('tenant_app');","handlingStrategy":"validation","validationCode":"function safeDbName(name: string): string {\n  if (typeof name === 'string' && name.includes('.')) {\n    throw new Error(`Invalid db name '${name}': dots are not allowed`);\n  }\n  return name;\n}\nconst db = client.db(safeDbName(input));","typeGuard":"function isValidDbName(name: unknown): name is string {\n  return typeof name === 'string' && name.length > 0 && !name.includes('.');\n}","tryCatchPattern":null,"preventionTips":["Sanitize db names derived from user/tenant input","Use kebab-case or snake_case for multi-tenant identifiers","Never pass a 'db.collection' namespace to client.db()"],"tags":["db","namespace","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}