{"id":"292704aeafdd89d8","repo":"mongodb/node-mongodb-native","slug":"cursor-document-did-not-contain-a-batch","errorCode":null,"errorMessage":"Cursor document did not contain a batch","messagePattern":"Cursor document did not contain a batch","errorType":"exception","errorClass":"MongoUnexpectedServerResponseError","httpStatus":null,"severity":"error","filePath":"src/cmap/wire_protocol/responses.ts","lineNumber":277,"sourceCode":"    if (namespace != null) return ns(namespace);\n    return null;\n  }\n\n  public get length() {\n    return Math.max(this.batchSize - this.iterated, 0);\n  }\n\n  private _encryptedBatch: OnDemandDocument | null = null;\n  get encryptedBatch() {\n    if (this.encryptedResponse == null) return null;\n    if (this._encryptedBatch != null) return this._encryptedBatch;\n\n    const cursor = this.encryptedResponse?.get('cursor', BSONType.object);\n    if (cursor?.has('firstBatch'))\n      this._encryptedBatch = cursor.get('firstBatch', BSONType.array, true);\n    else if (cursor?.has('nextBatch'))\n      this._encryptedBatch = cursor.get('nextBatch', BSONType.array, true);\n    else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');\n\n    return this._encryptedBatch;\n  }\n\n  private get batch() {\n    if (this._batch != null) return this._batch;\n    const cursor = this.cursor;\n    if (cursor.has('firstBatch')) this._batch = cursor.get('firstBatch', BSONType.array, true);\n    else if (cursor.has('nextBatch')) this._batch = cursor.get('nextBatch', BSONType.array, true);\n    else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');\n    return this._batch;\n  }\n\n  public get batchSize() {\n    return this.batch?.size();\n  }\n\n  public get postBatchResumeToken() {","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/wire_protocol/responses.ts#L259-L295","documentation":"Thrown by CursorResponse.encryptedBatch getter when, in a CSFLE/Queryable Encryption flow, the encrypted cursor document has neither a 'firstBatch' nor a 'nextBatch' array. The driver expects cursor responses to carry one of these batch fields; their absence means the (encrypted) server response is malformed. Surfaced as MongoUnexpectedServerResponseError.","triggerScenarios":"Client-side encryption is in use; the encrypted response from the server is parsed, its cursor subdocument is read, and neither firstBatch nor nextBatch is present. Happens with a malformed encrypted response, a server/proxy stripping batch fields, or a driver/encryption-library version mismatch that mis-parses the response.","commonSituations":"Using Queryable Encryption or CSFLE with an incompatible `mongodb-client-encryption` version, a server that does not support the encryption protocol, or an intermediary altering encrypted responses. Triggered on find/aggregate/getMore over an encrypted collection.","solutions":["Ensure `mongodb-client-encryption` (and its bundled libmongocrypt) versions are compatible with the driver version.","Confirm the MongoDB server supports CSFLE/Queryable Encryption for the operation being run.","Remove any proxy/LB that may alter encrypted responses.","Upgrade driver and encryption library to matching current versions."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await encryptedCollection.find({}).toArray();\n} catch (err) {\n  if (err instanceof MongoUnexpectedServerResponseError && /did not contain a batch/.test(err.message)) {\n    // encrypted response malformed; verify mongodb-client-encryption / libmongocrypt versions\n  }\n  throw err;\n}","preventionTips":["Keep mongodb-client-encryption (libmongocrypt) versions compatible with the driver.","Confirm the server supports CSFLE/Queryable Encryption for the operation.","Remove proxies that alter encrypted responses."],"tags":["csfle","queryable-encryption","cursor","server-response"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}