@@ -993,6 +993,7 @@ async def _update(
993993        session : Optional [AsyncClientSession ] =  None ,
994994        retryable_write : bool  =  False ,
995995        let : Optional [Mapping [str , Any ]] =  None ,
996+         sort : Optional [Mapping [str , Any ]] =  None ,
996997        comment : Optional [Any ] =  None ,
997998    ) ->  Optional [Mapping [str , Any ]]:
998999        """Internal update / replace helper.""" 
@@ -1024,6 +1025,14 @@ async def _update(
10241025            if  not  isinstance (hint , str ):
10251026                hint  =  helpers_shared ._index_document (hint )
10261027            update_doc ["hint" ] =  hint 
1028+         if  sort  is  not None :
1029+             if  not  acknowledged  and  conn .max_wire_version  <  25 :
1030+                 raise  ConfigurationError (
1031+                     "Must be connected to MongoDB 8.0+ to use sort on unacknowledged update commands." 
1032+                 )
1033+             common .validate_is_mapping ("sort" , sort )
1034+             update_doc ["sort" ] =  sort 
1035+ 
10271036        command  =  {"update" : self .name , "ordered" : ordered , "updates" : [update_doc ]}
10281037        if  let  is  not None :
10291038            common .validate_is_mapping ("let" , let )
@@ -1079,6 +1088,7 @@ async def _update_retryable(
10791088        hint : Optional [_IndexKeyHint ] =  None ,
10801089        session : Optional [AsyncClientSession ] =  None ,
10811090        let : Optional [Mapping [str , Any ]] =  None ,
1091+         sort : Optional [Mapping [str , Any ]] =  None ,
10821092        comment : Optional [Any ] =  None ,
10831093    ) ->  Optional [Mapping [str , Any ]]:
10841094        """Internal update / replace helper.""" 
@@ -1102,6 +1112,7 @@ async def _update(
11021112                session = session ,
11031113                retryable_write = retryable_write ,
11041114                let = let ,
1115+                 sort = sort ,
11051116                comment = comment ,
11061117            )
11071118
@@ -1122,6 +1133,7 @@ async def replace_one(
11221133        hint : Optional [_IndexKeyHint ] =  None ,
11231134        session : Optional [AsyncClientSession ] =  None ,
11241135        let : Optional [Mapping [str , Any ]] =  None ,
1136+         sort : Optional [Mapping [str , Any ]] =  None ,
11251137        comment : Optional [Any ] =  None ,
11261138    ) ->  UpdateResult :
11271139        """Replace a single document matching the filter. 
@@ -1176,8 +1188,13 @@ async def replace_one(
11761188            aggregate expression context (e.g. "$$var"). 
11771189        :param comment: A user-provided comment to attach to this 
11781190            command. 
1191+         :param sort: Specify which document the operation updates if the query matches 
1192+             multiple documents. The first document matched by the sort order will be updated. 
1193+             This option is only supported on MongoDB 8.0 and above. 
11791194        :return: - An instance of :class:`~pymongo.results.UpdateResult`. 
11801195
1196+         .. versionchanged:: 4.11 
1197+            Added ``sort`` parameter. 
11811198        .. versionchanged:: 4.1 
11821199           Added ``let`` parameter. 
11831200           Added ``comment`` parameter. 
@@ -1209,6 +1226,7 @@ async def replace_one(
12091226                hint = hint ,
12101227                session = session ,
12111228                let = let ,
1229+                 sort = sort ,
12121230                comment = comment ,
12131231            ),
12141232            write_concern .acknowledged ,
@@ -1225,6 +1243,7 @@ async def update_one(
12251243        hint : Optional [_IndexKeyHint ] =  None ,
12261244        session : Optional [AsyncClientSession ] =  None ,
12271245        let : Optional [Mapping [str , Any ]] =  None ,
1246+         sort : Optional [Mapping [str , Any ]] =  None ,
12281247        comment : Optional [Any ] =  None ,
12291248    ) ->  UpdateResult :
12301249        """Update a single document matching the filter. 
@@ -1283,11 +1302,16 @@ async def update_one(
12831302            constant or closed expressions that do not reference document 
12841303            fields. Parameters can then be accessed as variables in an 
12851304            aggregate expression context (e.g. "$$var"). 
1305+         :param sort: Specify which document the operation updates if the query matches 
1306+             multiple documents. The first document matched by the sort order will be updated. 
1307+             This option is only supported on MongoDB 8.0 and above. 
12861308        :param comment: A user-provided comment to attach to this 
12871309            command. 
12881310
12891311        :return: - An instance of :class:`~pymongo.results.UpdateResult`. 
12901312
1313+         .. versionchanged:: 4.11 
1314+            Added ``sort`` parameter. 
12911315        .. versionchanged:: 4.1 
12921316           Added ``let`` parameter. 
12931317           Added ``comment`` parameter. 
@@ -1322,6 +1346,7 @@ async def update_one(
13221346                hint = hint ,
13231347                session = session ,
13241348                let = let ,
1349+                 sort = sort ,
13251350                comment = comment ,
13261351            ),
13271352            write_concern .acknowledged ,
0 commit comments