Skip to content

Commit 86368fe

Browse files
fix bug
1 parent c3c6333 commit 86368fe

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

src/operations/update.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@ import { MongoAPIError, MongoCompatibilityError, MongoInvalidArgumentError, Mong
44
import type { InferIdType, TODO_NODE_3286 } from '../mongo_types';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
7+
import { formatSort, SortForCmd } from '../sort';
78
import { hasAtomicOperators, type MongoDBNamespace } from '../utils';
89
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
910
import { Aspect, defineAspects, type Hint } from './operation';
1011

11-
12-
/** @public */
13-
export interface UpdateOneOptions extends UpdateOptions {
14-
/**
15-
* Specify which document the operation updates if the query matches multiple
16-
* documents. The first document matched by the sort order will be updated.
17-
*
18-
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
19-
*/
20-
sort?: Document;
21-
}
22-
2312
/** @public */
2413
export interface UpdateOptions extends CommandOperationOptions {
2514
/** A set of filters specifying to which array elements an update should apply */
@@ -34,6 +23,15 @@ export interface UpdateOptions extends CommandOperationOptions {
3423
upsert?: boolean;
3524
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
3625
let?: Document;
26+
/**
27+
* Specify which document the operation updates if the query matches multiple
28+
* documents. The first document matched by the sort order will be updated.
29+
*
30+
* The server will report an error if the caller explicitly provides a value with updateMany().
31+
*
32+
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
33+
*/
34+
sort?: Document;
3735
}
3836

3937
/**
@@ -53,12 +51,6 @@ export interface UpdateResult<TSchema extends Document = Document> {
5351
upsertedId: InferIdType<TSchema> | null;
5452
}
5553

56-
/** @public */
57-
export interface UpdateOneStatement extends UpdateStatement {
58-
/** If the query matches multiple documents, the first document matched by the sort order will be updated. */
59-
sort?: Document;
60-
}
61-
6254
/** @public */
6355
export interface UpdateStatement {
6456
/** The query that matches documents to update. */
@@ -75,6 +67,8 @@ export interface UpdateStatement {
7567
arrayFilters?: Document[];
7668
/** A document or string that specifies the index to use to support the query predicate. */
7769
hint?: Hint;
70+
/** If the query matches multiple documents, the first document matched by the sort order will be updated. */
71+
sort?: SortForCmd;
7872
}
7973

8074
/**
@@ -147,7 +141,7 @@ export class UpdateOperation extends CommandOperation<Document> {
147141

148142
/** @internal */
149143
export class UpdateOneOperation extends UpdateOperation {
150-
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOneOptions) {
144+
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOptions) {
151145
super(
152146
collection.s.namespace,
153147
[makeUpdateStatement(filter, update, { ...options, multi: false })],
@@ -213,17 +207,6 @@ export class UpdateManyOperation extends UpdateOperation {
213207
}
214208
}
215209

216-
/** @public */
217-
export interface ReplaceOneOptions extends ReplaceOptions {
218-
/**
219-
* Specify which document the operation replaces if the query matches multiple
220-
* documents. The first document matched by the sort order will be replaced.
221-
*
222-
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
223-
*/
224-
sort?: Document;
225-
}
226-
227210
/** @public */
228211
export interface ReplaceOptions extends CommandOperationOptions {
229212
/** If true, allows the write to opt-out of document level validation */
@@ -236,6 +219,15 @@ export interface ReplaceOptions extends CommandOperationOptions {
236219
upsert?: boolean;
237220
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
238221
let?: Document;
222+
/**
223+
* Specify which document the operation replaces if the query matches multiple
224+
* documents. The first document matched by the sort order will be replaced.
225+
*
226+
* The server will report an error if the caller explicitly provides a value with replaceMany().
227+
*
228+
* This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
229+
*/
230+
sort?: Document;
239231
}
240232

241233
/** @internal */
@@ -244,7 +236,7 @@ export class ReplaceOneOperation extends UpdateOperation {
244236
collection: Collection,
245237
filter: Document,
246238
replacement: Document,
247-
options: ReplaceOneOptions
239+
options: ReplaceOptions
248240
) {
249241
super(
250242
collection.s.namespace,
@@ -280,8 +272,8 @@ export class ReplaceOneOperation extends UpdateOperation {
280272
export function makeUpdateStatement(
281273
filter: Document,
282274
update: Document | Document[],
283-
options: UpdateOneOptions & { multi?: boolean }
284-
): UpdateOneStatement {
275+
options: UpdateOptions & { multi?: boolean }
276+
): UpdateStatement {
285277
if (filter == null || typeof filter !== 'object') {
286278
throw new MongoInvalidArgumentError('Selector must be a valid JavaScript object');
287279
}
@@ -297,8 +289,10 @@ export function makeUpdateStatement(
297289

298290
if (options.multi) {
299291
op.multi = options.multi;
300-
} else {
301-
op.sort = options?.sort;
292+
}
293+
294+
if (options.sort) {
295+
op.sort = formatSort(options?.sort);
302296
}
303297

304298
if (options.hint) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ const loadBalancedCollationTests = [
5555
'Distinct with a collation'
5656
];
5757

58-
describe.only('CRUD unified', function () {
58+
describe('CRUD unified', function () {
5959
runUnifiedSuite(
6060
loadSpecTests(path.join('crud', 'unified')),
6161
({ description }, { isLoadBalanced }) => {
62-
return description === 'BulkWrite replaceOne with sort option unsupported (server-side error)'
63-
? false
64-
: 'skipping you';
62+
return description.match(clientBulkWriteTests)
63+
? 'TODO(NODE-6257): implement client level bulk write'
64+
: unacknowledgedHintTests.includes(description)
65+
? `TODO(NODE-3541)`
66+
: isLoadBalanced && loadBalancedCollationTests.includes(description)
67+
? `TODO(NODE-6280): fix collation for find and modify commands on load balanced mode`
68+
: false;
6569
}
6670
);
6771
});

0 commit comments

Comments
 (0)