{"id":"4df5ad2a9f897fe6","repo":"mongodb/node-mongodb-native","slug":"collection-this-namespace-not-found","errorCode":null,"errorMessage":"collection ${this.namespace} not found","messagePattern":"collection (.+?) not found","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/collection.ts","lineNumber":592,"sourceCode":"      this.s.namespace,\n      filter,\n      resolveOptions(this, options)\n    );\n  }\n\n  /**\n   * Returns the options of the collection.\n   *\n   * @param options - Optional settings for the command\n   */\n  async options(options?: OperationOptions): Promise<Document> {\n    options = resolveOptions(this, options);\n    const [collection] = await this.db\n      .listCollections({ name: this.collectionName }, { ...options, nameOnly: false })\n      .toArray();\n\n    if (collection == null || collection.options == null) {\n      throw new MongoAPIError(`collection ${this.namespace} not found`);\n    }\n\n    return collection.options;\n  }\n\n  /**\n   * Returns if the collection is a capped collection\n   *\n   * @param options - Optional settings for the command\n   */\n  async isCapped(options?: OperationOptions): Promise<boolean> {\n    const { capped } = await this.options(options);\n    return Boolean(capped);\n  }\n\n  /**\n   * Creates an index on the db and collection collection.\n   *","sourceCodeStart":574,"sourceCodeEnd":610,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/collection.ts#L574-L610","documentation":"Thrown by Collection.options() when listCollections returns no matching document, or the returned document has no options field. It is a MongoAPIError (note: despite the message text) emitted after a successful round-trip to the server, meaning the collection name does not exist in the database or is not a regular collection (e.g. a view, which has no 'options' field in the listCollections output).","triggerScenarios":"Calling collection.options() on a collection that was never created (MongoDB creates collections lazily on first write); calling it on a view; calling it against the wrong database name; a race where the collection was dropped between use and the options() call.","commonSituations":"Calling .options() or .isCapped() at startup to introspect a collection that has not been written yet; connecting to the wrong database in a multi-tenant deployment; collection name typo; calling isCapped() (which internally calls options()) on a non-existent collection.","solutions":["Confirm the collection exists: run db.listCollections({name: '<name>'}).toArray() in mongosh.","Ensure at least one document has been written, or explicitly create the collection with db.createCollection('<name>').","Verify the database name in the connection string / client.db() call.","If introspecting views, handle the missing options case rather than relying on options()."],"exampleFix":"// before\nconst opts = await db.collection('maybeMissing').options();\n// after\nconst [info] = await db.listCollections({ name: 'maybeMissing' }).toArray();\nif (!info) {\n  await db.createCollection('maybeMissing');\n}\nconst opts = await db.collection('maybeMissing').options();","handlingStrategy":"try-catch","validationCode":"async function collectionExists(db: Db, name: string): Promise<boolean> {\n  const list = await db.listCollections({ name }, { nameOnly: true }).toArray();\n  return list.length > 0;\n}\nif (!(await collectionExists(db, 'myColl'))) {\n  await db.createCollection('myColl');\n}\nconst opts = await db.collection('myColl').options();","typeGuard":null,"tryCatchPattern":"try {\n  return await collection.options();\n} catch (e) {\n  if (e instanceof MongoAPIError && /collection .* not found/.test(e.message)) {\n    return null; // treat missing as no options\n  }\n  throw e;\n}","preventionTips":["Create collections explicitly with createCollection before introspecting.","Verify the database name in client.db() matches the deployment.","Treat isCapped()/options() as best-effort and catch MongoAPIError."],"tags":["metadata","crud","collection","introspection"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}