{"record":{"id":"8f6b72bbaca946ab","repo":"mongodb/node-mongodb-native","slug":"unable-to-create-collection-cause-message","errorCode":null,"errorMessage":"Unable to create collection: ${cause.message}","messagePattern":"Unable to create collection: (.+?)","errorType":"exception","errorClass":"MongoCryptCreateEncryptedCollectionError","httpStatus":null,"severity":"error","filePath":"src/client-side-encryption/client_encryption.ts","lineNumber":635,"sourceCode":"      const rejection = createDataKeyResolutions.find(\n        (result): result is PromiseRejectedResult => result.status === 'rejected'\n      );\n      if (rejection != null) {\n        throw new MongoCryptCreateDataKeyError(encryptedFields, { cause: rejection.reason });\n      }\n    }\n\n    try {\n      const collection = await db.createCollection<TSchema>(name, {\n        ...createCollectionOptions,\n        encryptedFields,\n        timeoutMS: timeoutContext?.csotEnabled()\n          ? timeoutContext?.getRemainingTimeMSOrThrow()\n          : undefined\n      });\n      return { collection, encryptedFields };\n    } catch (cause) {\n      throw new MongoCryptCreateEncryptedCollectionError(encryptedFields, { cause });\n    }\n  }\n\n  /**\n   * Explicitly encrypt a provided value. Note that either `options.keyId` or `options.keyAltName` must\n   * be specified. Specifying both `options.keyId` and `options.keyAltName` is considered an error.\n   *\n   * @param value - The value that you wish to serialize. Must be of a type that can be serialized into BSON\n   * @param options -\n   * @returns a Promise that either resolves with the encrypted value, or rejects with an error.\n   *\n   * @example\n   * ```ts\n   * // Encryption with async/await api\n   * async function encryptMyData(value) {\n   *   const keyId = await clientEncryption.createDataKey('local');\n   *   return clientEncryption.encrypt(value, { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' });\n   * }","sourceCodeStart":617,"sourceCodeEnd":653,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/client-side-encryption/client_encryption.ts#L617-L653","documentation":"Thrown by ClientEncryption.createEncryptedCollection() when the underlying db.createCollection() call fails after data keys were successfully created. The error wraps the original cause and includes the full encryptedFields that were generated, so the caller can retry or clean up. This is a MongoCryptCreateEncryptedCollectionError.","triggerScenarios":"Data keys are created successfully, but the subsequent createCollection command fails. Common causes: the collection already exists, the encryptedFields configuration is invalid for the server version, insufficient permissions to create collections, or the server does not support encrypted collections.","commonSituations":"Running createEncryptedCollection when the collection already exists; server version too old for Queryable Encryption; invalid encryptedFields schema; MongoDB user lacks createCollection privileges; the encryptedFields configuration doesn't match what the server expects.","solutions":["Check error.cause for the specific server-side error (duplicate collection, permissions, etc.)","If the collection already exists, drop it first or use a different name","Verify the server version supports the encryption features you are using (CSFLE requires 4.2+, QE requires 7.0+)","Inspect error.encryptedFields to understand the generated configuration"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before calling createEncryptedCollection, check collection doesn't exist\nconst collections = await db.listCollections({ name }, { nameOnly: true }).toArray();\nif (collections.length > 0) {\n  throw new Error(`Collection ${name} already exists`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const result = await clientEncryption.createEncryptedCollection(db, name, options);\n} catch (error) {\n  if (error instanceof MongoCryptCreateEncryptedCollectionError) {\n    // Collection creation failed; data keys were already created\n    console.error('Collection creation failed:', error.cause?.message);\n    console.error('Generated encryptedFields:', error.encryptedFields);\n    // If collection exists, drop and retry or use existing\n  }\n}","preventionTips":["Check that the collection does not already exist before calling createEncryptedCollection","Verify the MongoDB user has createCollection privileges","Ensure the server version supports the encryption features in use (CSFLE 4.2+, QE 7.0+)","Validate the encryptedFields configuration matches the server's expected schema"],"tags":["csfle","client-encryption","encrypted-collection","server-error"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}