Skip to content

Commit f6aa204

Browse files
committed
fix: raw bson option
1 parent 62c9811 commit f6aa204

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,9 @@ export class ClientBulkWriteCursorResponse extends CursorResponse {
391391
return this.get('writeConcernError', BSONType.object, false);
392392
}
393393
}
394+
395+
export class ExplainResponse extends MongoDBResponse {
396+
get queryPlanner() {
397+
return this.get('queryPlanner', BSONType.object, false);
398+
}
399+
}

src/operations/find_one_operation.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type Db } from '..';
22
import { type Document, pluckBSONSerializeOptions } from '../bson';
33
import { type OnDemandDocumentDeserializeOptions } from '../cmap/wire_protocol/on_demand/document';
4+
import { CursorResponse, ExplainResponse } from '../cmap/wire_protocol/responses';
45
import type { Server } from '../sdam/server';
56
import type { ClientSession } from '../sessions';
67
import { type TimeoutContext } from '../timeout';
@@ -64,12 +65,25 @@ export class FindOneOperation<TSchema = any> extends CommandOperation<TSchema> {
6465
delete command.batchSize;
6566
}
6667

67-
const response = await super.executeCommand(server, session, command, timeoutContext);
68-
// In this case since we are just running a command, the response is a document with
69-
// a single batch cursor, not an OnDemandDocument. If we are explaining, we just
70-
// return the response as is.
71-
const document = this.explain ? response : (response.cursor?.firstBatch?.[0] ?? null);
72-
return document;
68+
if (this.explain) {
69+
const response = await super.executeCommand(
70+
server,
71+
session,
72+
command,
73+
timeoutContext,
74+
ExplainResponse
75+
);
76+
return response.toObject() as TSchema;
77+
} else {
78+
const response = await super.executeCommand(
79+
server,
80+
session,
81+
command,
82+
timeoutContext,
83+
CursorResponse
84+
);
85+
return response.shift(this.deserializationOptions);
86+
}
7387
}
7488
}
7589

0 commit comments

Comments
 (0)