Perform an atomic update on the fields matched by the query. :param upsert: insert if document doesn't exist (default ``False``) :param multi: Update multiple documents. :param write_concern: Extra keyword arguments are passed down which will be used as options f
(
self,
upsert=False,
multi=True,
write_concern=None,
read_concern=None,
full_result=False,
array_filters=None,
**update,
)
| 529 | return result.deleted_count |
| 530 | |
| 531 | def update( |
| 532 | self, |
| 533 | upsert=False, |
| 534 | multi=True, |
| 535 | write_concern=None, |
| 536 | read_concern=None, |
| 537 | full_result=False, |
| 538 | array_filters=None, |
| 539 | **update, |
| 540 | ): |
| 541 | """Perform an atomic update on the fields matched by the query. |
| 542 | |
| 543 | :param upsert: insert if document doesn't exist (default ``False``) |
| 544 | :param multi: Update multiple documents. |
| 545 | :param write_concern: Extra keyword arguments are passed down which |
| 546 | will be used as options for the resultant |
| 547 | ``getLastError`` command. For example, |
| 548 | ``save(..., write_concern={w: 2, fsync: True}, ...)`` will |
| 549 | wait until at least two servers have recorded the write and |
| 550 | will force an fsync on the primary server. |
| 551 | :param read_concern: Override the read concern for the operation |
| 552 | :param full_result: Return the associated ``pymongo.UpdateResult`` rather than just the number |
| 553 | updated items |
| 554 | :param array_filters: A list of filters specifying which array elements an update should apply. |
| 555 | :param update: Django-style update keyword arguments |
| 556 | |
| 557 | :returns the number of updated documents (unless ``full_result`` is True) |
| 558 | """ |
| 559 | if not update and not upsert: |
| 560 | raise OperationError("No update parameters, would remove data") |
| 561 | |
| 562 | if write_concern is None: |
| 563 | write_concern = {} |
| 564 | if self._none or self._empty: |
| 565 | return 0 |
| 566 | |
| 567 | queryset = self.clone() |
| 568 | query = queryset._query |
| 569 | if "__raw__" in update and isinstance( |
| 570 | update["__raw__"], list |
| 571 | ): # Case of Update with Aggregation Pipeline |
| 572 | update = [ |
| 573 | transform.update(queryset._document, **{"__raw__": u}) |
| 574 | for u in update["__raw__"] |
| 575 | ] |
| 576 | else: |
| 577 | update = transform.update(queryset._document, **update) |
| 578 | # If doing an atomic upsert on an inheritable class |
| 579 | # then ensure we add _cls to the update operation |
| 580 | if upsert and "_cls" in query: |
| 581 | if "$set" in update: |
| 582 | update["$set"]["_cls"] = queryset._document._class_name |
| 583 | else: |
| 584 | update["$set"] = {"_cls": queryset._document._class_name} |
| 585 | try: |
| 586 | with set_read_write_concern( |
| 587 | queryset._collection, write_concern, read_concern |
| 588 | ) as collection: |
no test coverage detected