Skip to content

Commit b850033

Browse files
support hint
1 parent 76b8324 commit b850033

File tree

4 files changed

+15
-53
lines changed

4 files changed

+15
-53
lines changed

src/operations/delete.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type Connection } from '../cmap/connection';
33
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
44
import { MongoCompatibilityError, MongoServerError } from '../error';
55
import type { ClientSession } from '../sessions';
6-
import { type MongoDBCollectionNamespace, type MongoDBNamespace } from '../utils';
6+
import { maxWireVersion, type MongoDBCollectionNamespace, type MongoDBNamespace } from '../utils';
77
import { type WriteConcernOptions } from '../write_concern';
88
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
99
import { Aspect, defineAspects, type Hint } from './operation';
@@ -65,7 +65,7 @@ export class DeleteOperation extends CommandOperation<Document> {
6565
return this.statements.every(op => (op.limit != null ? op.limit > 0 : true));
6666
}
6767

68-
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
68+
override buildCommandDocument(connection: Connection, _session?: ClientSession): Document {
6969
const options = this.options;
7070

7171
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
@@ -86,10 +86,11 @@ export class DeleteOperation extends CommandOperation<Document> {
8686
}
8787

8888
const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
89-
if (unacknowledgedWrite) {
89+
if (unacknowledgedWrite && maxWireVersion(connection) < 9) {
9090
if (this.statements.find((o: Document) => o.hint)) {
91-
// TODO(NODE-3541): fix error for hint with unacknowledged writes
92-
throw new MongoCompatibilityError(`hint is not supported with unacknowledged writes`);
91+
throw new MongoCompatibilityError(
92+
`hint for the delete command is only supported on MongoDB 4.4+`
93+
);
9394
}
9495
}
9596

src/operations/find_and_modify.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error';
66
import { ReadPreference } from '../read_preference';
77
import type { ClientSession } from '../sessions';
88
import { formatSort, type Sort, type SortForCmd } from '../sort';
9-
import { decorateWithCollation, hasAtomicOperators } from '../utils';
9+
import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
1010
import { type WriteConcern, type WriteConcernSettings } from '../write_concern';
1111
import { CommandOperation, type CommandOperationOptions } from './command';
1212
import { Aspect, defineAspects } from './operation';
@@ -146,7 +146,7 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
146146
}
147147

148148
override buildCommandDocument(
149-
_connection: Connection,
149+
connection: Connection,
150150
_session?: ClientSession
151151
): Document & FindAndModifyCmdBase {
152152
const options = this.options;
@@ -192,9 +192,9 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
192192

193193
if (options.hint) {
194194
const unacknowledgedWrite = this.writeConcern?.w === 0;
195-
if (unacknowledgedWrite) {
195+
if (unacknowledgedWrite && maxWireVersion(connection) < 9) {
196196
throw new MongoCompatibilityError(
197-
'The current topology does not support a hint on findAndModify commands'
197+
'hint for the findAndModify command is only supported on MongoDB 4.4+'
198198
);
199199
}
200200

src/operations/update.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { ClientSession } from '../sessions';
77
import { formatSort, type Sort, type SortForCmd } from '../sort';
88
import {
99
hasAtomicOperators,
10+
maxWireVersion,
1011
type MongoDBCollectionNamespace,
1112
type MongoDBNamespace
1213
} from '../utils';
@@ -121,14 +122,6 @@ export class UpdateOperation extends CommandOperation<Document> {
121122
command.comment = options.comment;
122123
}
123124

124-
const unacknowledgedWrite = this.writeConcern?.w === 0;
125-
if (unacknowledgedWrite) {
126-
if (this.statements.find((o: Document) => o.hint)) {
127-
// TODO(NODE-3541): fix error for hint with unacknowledged writes
128-
throw new MongoCompatibilityError(`hint is not supported with unacknowledged writes`);
129-
}
130-
}
131-
132125
return command;
133126
}
134127
}

test/integration/crud/crud.spec.test.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,6 @@ import * as path from 'path';
33
import { loadSpecTests } from '../../spec/index';
44
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
55

6-
const unacknowledgedHintTests = [
7-
'Unacknowledged updateOne with hint document on 4.2+ server',
8-
'Unacknowledged updateOne with hint string on 4.2+ server',
9-
'Unacknowledged updateMany with hint document on 4.2+ server',
10-
'Unacknowledged updateMany with hint string on 4.2+ server',
11-
'Unacknowledged replaceOne with hint document on 4.2+ server',
12-
'Unacknowledged replaceOne with hint string on 4.2+ server',
13-
'Unacknowledged updateOne with hint document on 4.2+ server',
14-
'Unacknowledged updateOne with hint string on 4.2+ server',
15-
'Unacknowledged updateMany with hint document on 4.2+ server',
16-
'Unacknowledged updateMany with hint string on 4.2+ server',
17-
'Unacknowledged replaceOne with hint document on 4.2+ server',
18-
'Unacknowledged replaceOne with hint string on 4.2+ server',
19-
'Unacknowledged findOneAndUpdate with hint document on 4.4+ server',
20-
'Unacknowledged findOneAndUpdate with hint string on 4.4+ server',
21-
'Unacknowledged findOneAndReplace with hint document on 4.4+ server',
22-
'Unacknowledged findOneAndReplace with hint string on 4.4+ server',
23-
'Unacknowledged findOneAndDelete with hint document on 4.4+ server',
24-
'Unacknowledged findOneAndDelete with hint string on 4.4+ server',
25-
'Unacknowledged deleteOne with hint document on 4.4+ server',
26-
'Unacknowledged deleteOne with hint string on 4.4+ server',
27-
'Unacknowledged deleteMany with hint document on 4.4+ server',
28-
'Unacknowledged deleteMany with hint string on 4.4+ server',
29-
'Unacknowledged deleteOne with hint document on 4.4+ server',
30-
'Unacknowledged deleteOne with hint string on 4.4+ server',
31-
'Unacknowledged deleteMany with hint document on 4.4+ server',
32-
'Unacknowledged deleteMany with hint string on 4.4+ server'
33-
];
34-
356
const loadBalancedCollationTests = [
367
'FindOneAndUpdate when many documents match with collation returning the document before modification',
378
'FindOneAndReplace when one document matches with collation returning the document after modification',
@@ -70,13 +41,10 @@ describe('CRUD unified', function () {
7041
runUnifiedSuite(
7142
loadSpecTests(path.join('crud', 'unified')),
7243
({ description }, { isLoadBalanced }) => {
73-
return unacknowledgedHintTests.includes(description)
74-
? `TODO(NODE-3541)`
75-
: isLoadBalanced && loadBalancedCollationTests.includes(description)
76-
? `TODO(NODE-6280): fix collation for find and modify commands on load balanced mode`
77-
: description in unimplementedCrudTests
78-
? unimplementedCrudTests[description]
79-
: false;
44+
if (isLoadBalanced && loadBalancedCollationTests.includes(description)) {
45+
return `TODO(NODE-6280): fix collation for find and modify commands on load balanced mode`;
46+
}
47+
return unimplementedCrudTests[description] ?? false;
8048
}
8149
);
8250
});

0 commit comments

Comments
 (0)