Update an existing document. Helper method, should only be used inside save().
(self, doc, save_condition, write_concern)
| 549 | return select_dict |
| 550 | |
| 551 | def _save_update(self, doc, save_condition, write_concern): |
| 552 | """Update an existing document. |
| 553 | |
| 554 | Helper method, should only be used inside save(). |
| 555 | """ |
| 556 | collection = self._get_collection() |
| 557 | object_id = doc["_id"] |
| 558 | created = False |
| 559 | |
| 560 | select_dict = {} |
| 561 | if save_condition is not None: |
| 562 | select_dict = transform.query(self.__class__, **save_condition) |
| 563 | |
| 564 | select_dict["_id"] = object_id |
| 565 | |
| 566 | select_dict = self._integrate_shard_key(doc, select_dict) |
| 567 | |
| 568 | update_doc = self._get_update_doc() |
| 569 | if update_doc: |
| 570 | upsert = save_condition is None |
| 571 | with set_write_concern(collection, write_concern) as wc_collection: |
| 572 | last_error = wc_collection.update_one( |
| 573 | select_dict, update_doc, upsert=upsert |
| 574 | ).raw_result |
| 575 | if not upsert and last_error["n"] == 0: |
| 576 | raise SaveConditionError( |
| 577 | "Race condition preventing document update detected" |
| 578 | ) |
| 579 | if last_error is not None: |
| 580 | updated_existing = last_error.get("updatedExisting") |
| 581 | if updated_existing is False: |
| 582 | created = True |
| 583 | # !!! This is bad, means we accidentally created a new, |
| 584 | # potentially corrupted document. See |
| 585 | # https://github.com/MongoEngine/mongoengine/issues/564 |
| 586 | |
| 587 | return object_id, created |
| 588 | |
| 589 | def cascade_save(self, **kwargs): |
| 590 | """Recursively save any references and generic references on the |
no test coverage detected