The return type for :meth:`~pymongo.collection.Collection.update_one`, :meth:`~pymongo.collection.Collection.update_many`, and :meth:`~pymongo.collection.Collection.replace_one`, and as part of :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
| 113 | |
| 114 | |
| 115 | class UpdateResult(_WriteResult): |
| 116 | """The return type for :meth:`~pymongo.collection.Collection.update_one`, |
| 117 | :meth:`~pymongo.collection.Collection.update_many`, and |
| 118 | :meth:`~pymongo.collection.Collection.replace_one`, and as part of |
| 119 | :meth:`~pymongo.mongo_client.MongoClient.bulk_write`. |
| 120 | """ |
| 121 | |
| 122 | __slots__ = ( |
| 123 | "__raw_result", |
| 124 | "__in_client_bulk", |
| 125 | ) |
| 126 | |
| 127 | def __init__( |
| 128 | self, |
| 129 | raw_result: Optional[Mapping[str, Any]], |
| 130 | acknowledged: bool, |
| 131 | in_client_bulk: bool = False, |
| 132 | ): |
| 133 | self.__raw_result = raw_result |
| 134 | self.__in_client_bulk = in_client_bulk |
| 135 | super().__init__(acknowledged) |
| 136 | |
| 137 | def __repr__(self) -> str: |
| 138 | return f"{self.__class__.__name__}({self.__raw_result!r}, acknowledged={self.acknowledged})" |
| 139 | |
| 140 | @property |
| 141 | def raw_result(self) -> Optional[Mapping[str, Any]]: |
| 142 | """The raw result document returned by the server.""" |
| 143 | return self.__raw_result |
| 144 | |
| 145 | @property |
| 146 | def matched_count(self) -> int: |
| 147 | """The number of documents matched for this update.""" |
| 148 | self._raise_if_unacknowledged("matched_count") |
| 149 | assert self.__raw_result is not None |
| 150 | if not self.__in_client_bulk and self.upserted_id is not None: |
| 151 | return 0 |
| 152 | return self.__raw_result.get("n", 0) |
| 153 | |
| 154 | @property |
| 155 | def modified_count(self) -> int: |
| 156 | """The number of documents modified.""" |
| 157 | self._raise_if_unacknowledged("modified_count") |
| 158 | assert self.__raw_result is not None |
| 159 | return cast(int, self.__raw_result.get("nModified")) |
| 160 | |
| 161 | @property |
| 162 | def upserted_id(self) -> Any: |
| 163 | """The _id of the inserted document if an upsert took place. Otherwise |
| 164 | ``None``. |
| 165 | """ |
| 166 | self._raise_if_unacknowledged("upserted_id") |
| 167 | assert self.__raw_result is not None |
| 168 | if self.__in_client_bulk and self.__raw_result.get("upserted"): |
| 169 | return self.__raw_result["upserted"]["_id"] |
| 170 | return self.__raw_result.get("upserted", None) |
| 171 | |
| 172 | @property |
no outgoing calls