- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.1k
PYTHON-4576 Allow update to supply sort option #1881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f7de5cd
              
                PYTHON-4576 Allow update to supply sort option
              
              
                blink1073 523875b
              
                clean up bulk support
              
              
                blink1073 2402cdf
              
                clean up operations
              
              
                blink1073 b63db31
              
                fix replaceone
              
              
                blink1073 ac2264f
              
                try with updated specs
              
              
                blink1073 7e46c4a
              
                update tests
              
              
                blink1073 6caba66
              
                Merge branch 'master' into PYTHON-4576
              
              
                blink1073 aa636d4
              
                Merge branch 'master' of github.com:mongodb/mongo-python-driver into …
              
              
                blink1073 14c2d80
              
                address review
              
              
                blink1073 978fa10
              
                Merge branch 'PYTHON-4576' of github.com:blink1073/mongo-python-drive…
              
              
                blink1073 ac64d2c
              
                address review
              
              
                blink1073 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -993,6 +993,7 @@ async def _update( | |
| session: Optional[AsyncClientSession] = None, | ||
| retryable_write: bool = False, | ||
| let: Optional[Mapping[str, Any]] = None, | ||
| sort: Optional[Mapping[str, Any]] = None, | ||
| comment: Optional[Any] = None, | ||
| ) -> Optional[Mapping[str, Any]]: | ||
| """Internal update / replace helper.""" | ||
|  | @@ -1024,6 +1025,14 @@ async def _update( | |
| if not isinstance(hint, str): | ||
| hint = helpers_shared._index_document(hint) | ||
| update_doc["hint"] = hint | ||
| if sort is not None: | ||
| if not acknowledged and conn.max_wire_version < 25: | ||
| raise ConfigurationError( | ||
| "Must be connected to MongoDB 8.0+ to use sort on unacknowledged update commands." | ||
| ) | ||
| common.validate_is_mapping("sort", sort) | ||
| update_doc["sort"] = sort | ||
|  | ||
| command = {"update": self.name, "ordered": ordered, "updates": [update_doc]} | ||
| if let is not None: | ||
| common.validate_is_mapping("let", let) | ||
|  | @@ -1079,6 +1088,7 @@ async def _update_retryable( | |
| hint: Optional[_IndexKeyHint] = None, | ||
| session: Optional[AsyncClientSession] = None, | ||
| let: Optional[Mapping[str, Any]] = None, | ||
| sort: Optional[Mapping[str, Any]] = None, | ||
| comment: Optional[Any] = None, | ||
| ) -> Optional[Mapping[str, Any]]: | ||
| """Internal update / replace helper.""" | ||
|  | @@ -1102,6 +1112,7 @@ async def _update( | |
| session=session, | ||
| retryable_write=retryable_write, | ||
| let=let, | ||
| sort=sort, | ||
| comment=comment, | ||
| ) | ||
|  | ||
|  | @@ -1122,6 +1133,7 @@ async def replace_one( | |
| hint: Optional[_IndexKeyHint] = None, | ||
| session: Optional[AsyncClientSession] = None, | ||
| let: Optional[Mapping[str, Any]] = None, | ||
| sort: Optional[Mapping[str, Any]] = None, | ||
| comment: Optional[Any] = None, | ||
| ) -> UpdateResult: | ||
| """Replace a single document matching the filter. | ||
|  | @@ -1176,8 +1188,13 @@ async def replace_one( | |
| aggregate expression context (e.g. "$$var"). | ||
| :param comment: A user-provided comment to attach to this | ||
| command. | ||
| :param sort: Specify which document the operation updates if the query matches | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a mention for these new apis in the changelog? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done | ||
| multiple documents. The first document matched by the sort order will be updated. | ||
|         
                  ShaneHarvey marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| This option is only supported on MongoDB 8.0 and above. | ||
| :return: - An instance of :class:`~pymongo.results.UpdateResult`. | ||
|  | ||
| .. versionchanged:: 4.11 | ||
| Added ``sort`` parameter. | ||
| .. versionchanged:: 4.1 | ||
| Added ``let`` parameter. | ||
| Added ``comment`` parameter. | ||
|  | @@ -1209,6 +1226,7 @@ async def replace_one( | |
| hint=hint, | ||
| session=session, | ||
| let=let, | ||
| sort=sort, | ||
| comment=comment, | ||
| ), | ||
| write_concern.acknowledged, | ||
|  | @@ -1225,6 +1243,7 @@ async def update_one( | |
| hint: Optional[_IndexKeyHint] = None, | ||
| session: Optional[AsyncClientSession] = None, | ||
| let: Optional[Mapping[str, Any]] = None, | ||
| sort: Optional[Mapping[str, Any]] = None, | ||
| comment: Optional[Any] = None, | ||
| ) -> UpdateResult: | ||
| """Update a single document matching the filter. | ||
|  | @@ -1283,11 +1302,16 @@ async def update_one( | |
| constant or closed expressions that do not reference document | ||
| fields. Parameters can then be accessed as variables in an | ||
| aggregate expression context (e.g. "$$var"). | ||
| :param sort: Specify which document the operation updates if the query matches | ||
| multiple documents. The first document matched by the sort order will be updated. | ||
| This option is only supported on MongoDB 8.0 and above. | ||
| :param comment: A user-provided comment to attach to this | ||
| command. | ||
|  | ||
| :return: - An instance of :class:`~pymongo.results.UpdateResult`. | ||
|  | ||
| .. versionchanged:: 4.11 | ||
| Added ``sort`` parameter. | ||
| .. versionchanged:: 4.1 | ||
| Added ``let`` parameter. | ||
| Added ``comment`` parameter. | ||
|  | @@ -1322,6 +1346,7 @@ async def update_one( | |
| hint=hint, | ||
| session=session, | ||
| let=let, | ||
| sort=sort, | ||
| comment=comment, | ||
| ), | ||
| write_concern.acknowledged, | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is slightly inconsistent with pymongo's other sort APIs which use
_Sorttype and allow a list of key pairs as well as a mapping. I think that's okay because mapping should be the preferred approach now (after PYTHON-2878).I say we keep it like this and only add support for a list of pairs if a user requests it. It will save us the work of adding tests for the (list or pairs) behavior.