-
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
Changes from 7 commits
f7de5cd
523875b
2402cdf
b63db31
ac2264f
7e46c4a
6caba66
aa636d4
14c2d80
978fa10
ac64d2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,12 @@ 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
|
||
:return: - An instance of :class:`~pymongo.results.UpdateResult`. | ||
|
||
.. versionchanged:: 4.10 | ||
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. 4.10 -> 4.11 here and elsewhere. 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 |
||
Added ``sort`` parameter. | ||
.. versionchanged:: 4.1 | ||
Added ``let`` parameter. | ||
Added ``comment`` parameter. | ||
|
@@ -1209,6 +1225,7 @@ async def replace_one( | |
hint=hint, | ||
session=session, | ||
let=let, | ||
sort=sort, | ||
comment=comment, | ||
), | ||
write_concern.acknowledged, | ||
|
@@ -1225,6 +1242,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 +1301,15 @@ 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. | ||
:param comment: A user-provided comment to attach to this | ||
command. | ||
|
||
:return: - An instance of :class:`~pymongo.results.UpdateResult`. | ||
|
||
.. versionchanged:: 4.10 | ||
Added ``sort`` parameter. | ||
.. versionchanged:: 4.1 | ||
Added ``let`` parameter. | ||
Added ``comment`` parameter. | ||
|
@@ -1322,6 +1344,7 @@ async def update_one( | |
hint=hint, | ||
session=session, | ||
let=let, | ||
sort=sort, | ||
comment=comment, | ||
), | ||
write_concern.acknowledged, | ||
|
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
_Sort
type 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.