mongodb/node-mongodb-native · error · MongoUnexpectedServerResponseError
Cursor document did not contain a batch
Error message
Cursor document did not contain a batch
What it means
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.
Source
Thrown at src/cmap/wire_protocol/responses.ts:277
if (namespace != null) return ns(namespace);
return null;
}
public get length() {
return Math.max(this.batchSize - this.iterated, 0);
}
private _encryptedBatch: OnDemandDocument | null = null;
get encryptedBatch() {
if (this.encryptedResponse == null) return null;
if (this._encryptedBatch != null) return this._encryptedBatch;
const cursor = this.encryptedResponse?.get('cursor', BSONType.object);
if (cursor?.has('firstBatch'))
this._encryptedBatch = cursor.get('firstBatch', BSONType.array, true);
else if (cursor?.has('nextBatch'))
this._encryptedBatch = cursor.get('nextBatch', BSONType.array, true);
else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');
return this._encryptedBatch;
}
private get batch() {
if (this._batch != null) return this._batch;
const cursor = this.cursor;
if (cursor.has('firstBatch')) this._batch = cursor.get('firstBatch', BSONType.array, true);
else if (cursor.has('nextBatch')) this._batch = cursor.get('nextBatch', BSONType.array, true);
else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');
return this._batch;
}
public get batchSize() {
return this.batch?.size();
}
public get postBatchResumeToken() {View on GitHub (pinned to 3366c21a63)
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.
Defensive patterns
Strategy: try-catch
Try / catch
try {
await encryptedCollection.find({}).toArray();
} catch (err) {
if (err instanceof MongoUnexpectedServerResponseError && /did not contain a batch/.test(err.message)) {
// encrypted response malformed; verify mongodb-client-encryption / libmongocrypt versions
}
throw err;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to create collection: ${cause.message}
- Malformed JSON body in GET request.
- Unable to complete request.
- Malformed response body - missing field `access_token`.
- Malformed response body - missing field `expires_in`.
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/292704aeafdd89d8.json.
Report an issue: GitHub.