@@ -490,6 +490,61 @@ describe('Shell API (integration)', function () {
490490 } ) ;
491491 } ) ;
492492
493+ describe ( 'updateOne and replaceOne with sort option' , function ( ) {
494+ skipIfServerVersion ( testServer , '< 8.0' ) ;
495+
496+ beforeEach ( async function ( ) {
497+ await serviceProvider . insertMany ( dbName , collectionName , [
498+ { _id : 1 , category : 'A' , score : 20 , order : 1 } ,
499+ { _id : 2 , category : 'A' , score : 10 , order : 2 } ,
500+ { _id : 3 , category : 'A' , score : 15 , order : 3 } ,
501+ { _id : 4 , category : 'B' , score : 25 , order : 1 } ,
502+ ] ) ;
503+ } ) ;
504+
505+ it ( 'updates the first document based on sort order' , async function ( ) {
506+ const result = await collection . updateOne (
507+ { category : 'A' } ,
508+ { $set : { updated : true } } ,
509+ { sort : { score : 1 } }
510+ ) ;
511+
512+ expect ( result . matchedCount ) . to . equal ( 1 ) ;
513+ expect ( result . modifiedCount ) . to . equal ( 1 ) ;
514+
515+ // Should have updated the document with the lowest score (_id: 2, score: 10)
516+ const categoryDocs = await serviceProvider
517+ . find ( dbName , collectionName , { category : 'A' } )
518+ . toArray ( ) ;
519+ const updatedDocs = categoryDocs . filter ( ( doc ) => doc . updated ) ;
520+ expect ( updatedDocs ) . to . have . lengthOf ( 1 ) ;
521+ expect ( updatedDocs [ 0 ] . _id ) . to . equal ( 2 ) ;
522+ expect ( updatedDocs [ 0 ] . score ) . to . equal ( 10 ) ;
523+ } ) ;
524+
525+ it ( 'replaces the first document based on sort order' , async function ( ) {
526+ const result = await collection . replaceOne (
527+ { category : 'A' } ,
528+ { replaced : true , category : 'A' } ,
529+ { sort : { score : 1 } }
530+ ) ;
531+
532+ expect ( result . matchedCount ) . to . equal ( 1 ) ;
533+ expect ( result . modifiedCount ) . to . equal ( 1 ) ;
534+
535+ // Should have replaced the document with the lowest score (_id: 2, score: 10)
536+ const categoryDocs = await serviceProvider
537+ . find ( dbName , collectionName , { category : 'A' } )
538+ . toArray ( ) ;
539+ const updatedDocs = categoryDocs . filter ( ( doc ) => doc . replaced ) ;
540+ expect ( updatedDocs ) . to . have . lengthOf ( 1 ) ;
541+ expect ( updatedDocs [ 0 ] . _id ) . to . equal ( 2 ) ;
542+ expect ( updatedDocs [ 0 ] . replaced ) . to . be . true ;
543+ expect ( updatedDocs [ 0 ] . score ) . to . undefined ;
544+ expect ( updatedDocs [ 0 ] . order ) . to . undefined ;
545+ } ) ;
546+ } ) ;
547+
493548 describe ( 'convertToCapped' , function ( ) {
494549 skipIfApiStrict ( ) ;
495550 let result : Document ;
0 commit comments