@@ -4,22 +4,11 @@ import { MongoAPIError, MongoCompatibilityError, MongoInvalidArgumentError, Mong
4
4
import type { InferIdType , TODO_NODE_3286 } from '../mongo_types' ;
5
5
import type { Server } from '../sdam/server' ;
6
6
import type { ClientSession } from '../sessions' ;
7
+ import { formatSort , SortForCmd } from '../sort' ;
7
8
import { hasAtomicOperators , type MongoDBNamespace } from '../utils' ;
8
9
import { type CollationOptions , CommandOperation , type CommandOperationOptions } from './command' ;
9
10
import { Aspect , defineAspects , type Hint } from './operation' ;
10
11
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
-
23
12
/** @public */
24
13
export interface UpdateOptions extends CommandOperationOptions {
25
14
/** A set of filters specifying to which array elements an update should apply */
@@ -34,6 +23,15 @@ export interface UpdateOptions extends CommandOperationOptions {
34
23
upsert ?: boolean ;
35
24
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
36
25
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 ;
37
35
}
38
36
39
37
/**
@@ -53,12 +51,6 @@ export interface UpdateResult<TSchema extends Document = Document> {
53
51
upsertedId : InferIdType < TSchema > | null ;
54
52
}
55
53
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
-
62
54
/** @public */
63
55
export interface UpdateStatement {
64
56
/** The query that matches documents to update. */
@@ -75,6 +67,8 @@ export interface UpdateStatement {
75
67
arrayFilters ?: Document [ ] ;
76
68
/** A document or string that specifies the index to use to support the query predicate. */
77
69
hint ?: Hint ;
70
+ /** If the query matches multiple documents, the first document matched by the sort order will be updated. */
71
+ sort ?: SortForCmd ;
78
72
}
79
73
80
74
/**
@@ -147,7 +141,7 @@ export class UpdateOperation extends CommandOperation<Document> {
147
141
148
142
/** @internal */
149
143
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 ) {
151
145
super (
152
146
collection . s . namespace ,
153
147
[ makeUpdateStatement ( filter , update , { ...options , multi : false } ) ] ,
@@ -213,17 +207,6 @@ export class UpdateManyOperation extends UpdateOperation {
213
207
}
214
208
}
215
209
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
-
227
210
/** @public */
228
211
export interface ReplaceOptions extends CommandOperationOptions {
229
212
/** If true, allows the write to opt-out of document level validation */
@@ -236,6 +219,15 @@ export interface ReplaceOptions extends CommandOperationOptions {
236
219
upsert ?: boolean ;
237
220
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
238
221
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 ;
239
231
}
240
232
241
233
/** @internal */
@@ -244,7 +236,7 @@ export class ReplaceOneOperation extends UpdateOperation {
244
236
collection : Collection ,
245
237
filter : Document ,
246
238
replacement : Document ,
247
- options : ReplaceOneOptions
239
+ options : ReplaceOptions
248
240
) {
249
241
super (
250
242
collection . s . namespace ,
@@ -280,8 +272,8 @@ export class ReplaceOneOperation extends UpdateOperation {
280
272
export function makeUpdateStatement (
281
273
filter : Document ,
282
274
update : Document | Document [ ] ,
283
- options : UpdateOneOptions & { multi ?: boolean }
284
- ) : UpdateOneStatement {
275
+ options : UpdateOptions & { multi ?: boolean }
276
+ ) : UpdateStatement {
285
277
if ( filter == null || typeof filter !== 'object' ) {
286
278
throw new MongoInvalidArgumentError ( 'Selector must be a valid JavaScript object' ) ;
287
279
}
@@ -297,8 +289,10 @@ export function makeUpdateStatement(
297
289
298
290
if ( options . multi ) {
299
291
op . multi = options . multi ;
300
- } else {
301
- op . sort = options ?. sort ;
292
+ }
293
+
294
+ if ( options . sort ) {
295
+ op . sort = formatSort ( options ?. sort ) ;
302
296
}
303
297
304
298
if ( options . hint ) {
0 commit comments