Overwrite or add the first document matched by the query. :param write_concern: Extra keyword arguments are passed down which will be used as options for the resultant ``getLastError`` command. For example, ``save(..., write_concern={w: 2, fsync: True},
(self, write_concern=None, read_concern=None, **update)
| 605 | raise OperationError("Update failed (%s)" % err) |
| 606 | |
| 607 | def upsert_one(self, write_concern=None, read_concern=None, **update): |
| 608 | """Overwrite or add the first document matched by the query. |
| 609 | |
| 610 | :param write_concern: Extra keyword arguments are passed down which |
| 611 | will be used as options for the resultant |
| 612 | ``getLastError`` command. For example, |
| 613 | ``save(..., write_concern={w: 2, fsync: True}, ...)`` will |
| 614 | wait until at least two servers have recorded the write and |
| 615 | will force an fsync on the primary server. |
| 616 | :param read_concern: Override the read concern for the operation |
| 617 | :param update: Django-style update keyword arguments |
| 618 | |
| 619 | :returns the new or overwritten document |
| 620 | """ |
| 621 | |
| 622 | atomic_update = self.update( |
| 623 | multi=False, |
| 624 | upsert=True, |
| 625 | write_concern=write_concern, |
| 626 | read_concern=read_concern, |
| 627 | full_result=True, |
| 628 | **update, |
| 629 | ) |
| 630 | |
| 631 | if atomic_update.raw_result["updatedExisting"]: |
| 632 | document = self.get() |
| 633 | else: |
| 634 | document = self._document.objects.with_id(atomic_update.upserted_id) |
| 635 | return document |
| 636 | |
| 637 | def update_one( |
| 638 | self, |