@@ -4,22 +4,11 @@ import { MongoAPIError, MongoCompatibilityError, MongoInvalidArgumentError, Mong
44import type { InferIdType , TODO_NODE_3286 } from '../mongo_types' ;
55import type { Server } from '../sdam/server' ;
66import type { ClientSession } from '../sessions' ;
7+ import { formatSort , SortForCmd } from '../sort' ;
78import { hasAtomicOperators , type MongoDBNamespace } from '../utils' ;
89import { type CollationOptions , CommandOperation , type CommandOperationOptions } from './command' ;
910import { 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 */
2413export 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 */
6355export 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 */
149143export 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 */
228211export 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 {
280272export 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 ) {
0 commit comments