Skip to content

Commit 13556ef

Browse files
find
1 parent 1e00a4f commit 13556ef

File tree

3 files changed

+41
-81
lines changed

3 files changed

+41
-81
lines changed

src/operations/find.ts

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import type { Document } from '../bson';
22
import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses';
33
import { type AbstractCursorOptions, type CursorTimeoutMode } from '../cursor/abstract_cursor';
44
import { MongoInvalidArgumentError } from '../error';
5-
import {
6-
decorateWithExplain,
7-
type ExplainOptions,
8-
validateExplainTimeoutOptions
9-
} from '../explain';
10-
import { ReadConcern } from '../read_concern';
11-
import type { Server } from '../sdam/server';
12-
import type { ClientSession } from '../sessions';
5+
import { type ExplainOptions } from '../explain';
6+
import type { ServerCommandOptions } from '../sdam/server';
137
import { formatSort, type Sort } from '../sort';
148
import { type TimeoutContext } from '../timeout';
159
import { type MongoDBNamespace, normalizeHintField } from '../utils';
16-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
10+
import {
11+
type CollationOptions,
12+
type CommandOperationOptions,
13+
ModernizedCommandOperation
14+
} from './command';
1715
import { Aspect, defineAspects, type Hint } from './operation';
1816

1917
/**
@@ -92,7 +90,9 @@ export interface FindOneOptions extends FindOptions {
9290
}
9391

9492
/** @internal */
95-
export class FindOperation extends CommandOperation<CursorResponse> {
93+
export class FindOperation extends ModernizedCommandOperation<CursorResponse> {
94+
override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse;
95+
9696
/**
9797
* @remarks WriteConcern can still be present on the options because
9898
* we inherit options from the client/db/collection. The
@@ -116,39 +116,32 @@ export class FindOperation extends CommandOperation<CursorResponse> {
116116

117117
// special case passing in an ObjectId as a filter
118118
this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter;
119+
120+
this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse;
119121
}
120122

121123
override get commandName() {
122124
return 'find' as const;
123125
}
124126

125-
override async execute(
126-
server: Server,
127-
session: ClientSession | undefined,
128-
timeoutContext: TimeoutContext
129-
): Promise<CursorResponse> {
130-
this.server = server;
131-
132-
const options = this.options;
127+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
128+
return {
129+
...this.options,
130+
...this.bsonOptions,
131+
documentsReturnedIn: 'firstBatch',
132+
session: this.session,
133+
timeoutContext
134+
};
135+
}
133136

134-
let findCommand = makeFindCommand(this.ns, this.filter, options);
135-
if (this.explain) {
136-
validateExplainTimeoutOptions(this.options, this.explain);
137-
findCommand = decorateWithExplain(findCommand, this.explain);
138-
}
137+
override handleOk(
138+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
139+
): CursorResponse {
140+
return response;
141+
}
139142

140-
return await server.command(
141-
this.ns,
142-
findCommand,
143-
{
144-
...this.options,
145-
...this.bsonOptions,
146-
documentsReturnedIn: 'firstBatch',
147-
session,
148-
timeoutContext
149-
},
150-
this.explain ? ExplainedCursorResponse : CursorResponse
151-
);
143+
override buildCommandDocument(): Document {
144+
return makeFindCommand(this.ns, this.filter, this.options);
152145
}
153146
}
154147

@@ -217,15 +210,6 @@ function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOp
217210
findCommand.comment = options.comment;
218211
}
219212

220-
if (typeof options.maxTimeMS === 'number') {
221-
findCommand.maxTimeMS = options.maxTimeMS;
222-
}
223-
224-
const readConcern = ReadConcern.fromOptions(options);
225-
if (readConcern) {
226-
findCommand.readConcern = readConcern.toJSON();
227-
}
228-
229213
if (options.max) {
230214
findCommand.max = options.max;
231215
}
@@ -263,11 +247,6 @@ function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOp
263247
if (typeof options.allowPartialResults === 'boolean') {
264248
findCommand.allowPartialResults = options.allowPartialResults;
265249
}
266-
267-
if (options.collation) {
268-
findCommand.collation = options.collation;
269-
}
270-
271250
if (typeof options.allowDiskUse === 'boolean') {
272251
findCommand.allowDiskUse = options.allowDiskUse;
273252
}

test/integration/server-selection/operation_count.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Server Operation Count Tests', function () {
7070
it('is zero after a successful command', loadBalancedTestMetadata, async function () {
7171
const server = Array.from(client.topology.s.servers.values())[0];
7272
expect(server.s.operationCount).to.equal(0);
73-
const commandSpy = sinon.spy(server, 'command');
73+
const commandSpy = sinon.spy(server, 'modernCommand');
7474

7575
await collection.findOne({ count: 1 });
7676

@@ -84,7 +84,7 @@ describe('Server Operation Count Tests', function () {
8484
const server = Array.from(client.topology.s.servers.values())[0];
8585
expect(server.s.operationCount).to.equal(0);
8686

87-
const commandSpy = sinon.spy(server, 'command');
87+
const commandSpy = sinon.spy(server, 'modernCommand');
8888

8989
const error = await collection.findOne({ count: 1 }).catch(e => e);
9090

@@ -104,7 +104,7 @@ describe('Server Operation Count Tests', function () {
104104
sinon
105105
.stub(ConnectionPool.prototype, 'checkOut')
106106
.rejects(new Error('unable to checkout connection'));
107-
const commandSpy = sinon.spy(server, 'command');
107+
const commandSpy = sinon.spy(server, 'modernCommand');
108108

109109
const error = await collection.findOne({ count: 1 }).catch(e => e);
110110

test/unit/operations/find.test.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
33

4-
import { FindOperation, ns, Server, ServerDescription } from '../../mongodb';
5-
import { topologyWithPlaceholderClient } from '../../tools/utils';
4+
import { FindOperation, ns } from '../../mongodb';
65

76
describe('FindOperation', function () {
87
const namespace = ns('db.coll');
@@ -33,34 +32,16 @@ describe('FindOperation', function () {
3332
});
3433
});
3534

36-
describe('#execute', function () {
37-
context('command construction', () => {
38-
const namespace = ns('db.collection');
39-
const topology = topologyWithPlaceholderClient([], {} as any);
40-
const server = new Server(topology, new ServerDescription('a:1'), {} as any);
35+
context('command construction', () => {
36+
const namespace = ns('db.collection');
4137

42-
it('should build basic find command with filter', async () => {
43-
const findOperation = new FindOperation(namespace, filter);
44-
const stub = sinon.stub(server, 'command').resolves({});
45-
await findOperation.execute(server, undefined);
46-
expect(stub).to.have.been.calledOnceWith(namespace, {
47-
find: namespace.collection,
48-
filter
49-
});
50-
});
51-
52-
it('should build find command with oplogReplay', async () => {
53-
const options = {
54-
oplogReplay: true
55-
};
56-
const findOperation = new FindOperation(namespace, {}, options);
57-
const stub = sinon.stub(server, 'command').resolves({});
58-
await findOperation.execute(server, undefined);
59-
expect(stub).to.have.been.calledOnceWith(
60-
namespace,
61-
sinon.match.has('oplogReplay', options.oplogReplay)
62-
);
63-
});
38+
it('should build find command with oplogReplay', () => {
39+
const options = {
40+
oplogReplay: true
41+
};
42+
const findOperation = new FindOperation(namespace, {}, options);
43+
const command = findOperation.buildCommandDocument();
44+
expect(command.oplogReplay).to.be.true;
6445
});
6546
});
6647
});

0 commit comments

Comments
 (0)