MCPcopy Create free account
hub / github.com/datastax/astrapy / replace_one

Method replace_one

astrapy/data/collection.py:5069–5186  ·  view source on GitHub ↗

Replace a single document on the collection with a new one, optionally inserting a new one if no match is found. Args: filter: a predicate expressed as a dictionary according to the Data API filter syntax. Examples are: {}

(
        self,
        filter: FilterType,
        replacement: DOC,
        *,
        sort: SortType | None = None,
        upsert: bool = False,
        general_method_timeout_ms: int | None = None,
        request_timeout_ms: int | None = None,
        timeout_ms: int | None = None,
    )

Source from the content-addressed store, hash-verified

5067 )
5068
5069 async def replace_one(
5070 self,
5071 filter: FilterType,
5072 replacement: DOC,
5073 *,
5074 sort: SortType | None = None,
5075 upsert: bool = False,
5076 general_method_timeout_ms: int | None = None,
5077 request_timeout_ms: int | None = None,
5078 timeout_ms: int | None = None,
5079 ) -> CollectionUpdateResult:
5080 """
5081 Replace a single document on the collection with a new one,
5082 optionally inserting a new one if no match is found.
5083
5084 Args:
5085 filter: a predicate expressed as a dictionary according to the
5086 Data API filter syntax. Examples are:
5087 {}
5088 {"name": "John"}
5089 {"price": {"$lt": 100}}
5090 {"$and": [{"name": "John"}, {"price": {"$lt": 100}}]}
5091 See the Data API documentation for the full set of operators.
5092 replacement: the new document to write into the collection.
5093 sort: with this dictionary parameter one can control the sorting
5094 order of the documents matching the filter, effectively
5095 determining what document will come first and hence be the
5096 replaced one. See the `find` method for more on sorting.
5097 Vector-based ANN sorting is achieved by providing a "$vector"
5098 or a "$vectorize" key in `sort`.
5099 upsert: this parameter controls the behavior in absence of matches.
5100 If True, `replacement` is inserted as a new document
5101 if no matches are found on the collection. If False,
5102 the operation silently does nothing in case of no matches.
5103 general_method_timeout_ms: a timeout, in milliseconds, to impose on the
5104 underlying API request. If not provided, this object's defaults apply.
5105 (This method issues a single API request, hence all timeout parameters
5106 are treated the same.)
5107 request_timeout_ms: an alias for `general_method_timeout_ms`.
5108 timeout_ms: an alias for `general_method_timeout_ms`.
5109
5110 Returns:
5111 a CollectionUpdateResult object summarizing the outcome of
5112 the replace operation.
5113
5114 Example:
5115 >>> # NOTE: may require slight adaptation to an async context.
5116 >>>
5117 >>> async def do_replace_one(acol: AsyncCollection) -> None:
5118 ... await acol.insert_one({"Marco": "Polo"})
5119 ... result0 = await acol.replace_one(
5120 ... {"Marco": {"$exists": True}},
5121 ... {"Buda": "Pest"},
5122 ... )
5123 ... print("result0.update_info", result0.update_info)
5124 ... doc1 = await acol.find_one({"Buda": "Pest"})
5125 ... print("doc1", doc1)
5126 ... result1 = await acol.replace_one(

Callers

nothing calls this directly

Calls 7

_converted_requestMethod · 0.95
_TimeoutContextClass · 0.90
_prepare_update_infoFunction · 0.85
infoMethod · 0.45

Tested by

no test coverage detected