|
29 | 29 | use MongoDB\Operation\InsertMany;
|
30 | 30 | use MongoDB\Operation\InsertOne;
|
31 | 31 | use MongoDB\Operation\ListIndexes;
|
| 32 | +use MongoDB\Operation\ReplaceOne; |
| 33 | +use MongoDB\Operation\UpdateMany; |
| 34 | +use MongoDB\Operation\UpdateOne; |
32 | 35 | use Traversable;
|
33 | 36 |
|
34 | 37 | class Collection
|
@@ -609,79 +612,68 @@ public function listIndexes(array $options = array())
|
609 | 612 | }
|
610 | 613 |
|
611 | 614 | /**
|
612 |
| - * Replace one document |
| 615 | + * Replaces at most one document matching the filter. |
613 | 616 | *
|
| 617 | + * @see ReplaceOne::__construct() for supported options |
614 | 618 | * @see http://docs.mongodb.org/manual/reference/command/update/
|
615 |
| - * @see Collection::getWriteOptions() for supported $options |
616 |
| - * |
617 |
| - * @param array $filter The document to be replaced |
618 |
| - * @param array $update The document to replace with |
619 |
| - * @param array $options Additional options |
| 619 | + * @param array|object $filter Query by which to filter documents |
| 620 | + * @param array|object $replacement Replacement document |
| 621 | + * @param array $options Command options |
620 | 622 | * @return UpdateResult
|
621 | 623 | */
|
622 |
| - public function replaceOne(array $filter, array $update, array $options = array()) |
| 624 | + public function replaceOne($filter, $replacement, array $options = array()) |
623 | 625 | {
|
624 |
| - $firstKey = key($update); |
625 |
| - if (isset($firstKey[0]) && $firstKey[0] == '$') { |
626 |
| - throw new InvalidArgumentException("First key in \$update must NOT be a \$operator"); |
| 626 | + if ( ! isset($options['writeConcern']) && isset($this->wc)) { |
| 627 | + $options['writeConcern'] = $this->wc; |
627 | 628 | }
|
628 |
| - $wr = $this->_update($filter, $update, $options + array("multi" => false)); |
629 | 629 |
|
630 |
| - return new UpdateResult($wr); |
| 630 | + $operation = new ReplaceOne($this->dbname, $this->collname, $filter, $replacement, $options); |
| 631 | + $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); |
| 632 | + |
| 633 | + return $operation->execute($server); |
631 | 634 | }
|
632 | 635 |
|
633 | 636 | /**
|
634 |
| - * Update one document |
635 |
| - * NOTE: Will update ALL documents matching $filter |
| 637 | + * Updates all documents matching the filter. |
636 | 638 | *
|
| 639 | + * @see UpdateMany::__construct() for supported options |
637 | 640 | * @see http://docs.mongodb.org/manual/reference/command/update/
|
638 |
| - * @see Collection::getWriteOptions() for supported $options |
639 |
| - * |
640 |
| - * @param array $filter The document to be replaced |
641 |
| - * @param array $update An array of update operators to apply to the document |
642 |
| - * @param array $options Additional options |
| 641 | + * @param array|object $filter Query by which to filter documents |
| 642 | + * @param array|object $replacement Update to apply to the matched documents |
| 643 | + * @param array $options Command options |
643 | 644 | * @return UpdateResult
|
644 | 645 | */
|
645 |
| - public function updateMany(array $filter, $update, array $options = array()) |
| 646 | + public function updateMany($filter, $update, array $options = array()) |
646 | 647 | {
|
647 |
| - $wr = $this->_update($filter, $update, $options + array("multi" => true)); |
| 648 | + if ( ! isset($options['writeConcern']) && isset($this->wc)) { |
| 649 | + $options['writeConcern'] = $this->wc; |
| 650 | + } |
648 | 651 |
|
649 |
| - return new UpdateResult($wr); |
| 652 | + $operation = new UpdateMany($this->dbname, $this->collname, $filter, $update, $options); |
| 653 | + $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); |
| 654 | + |
| 655 | + return $operation->execute($server); |
650 | 656 | }
|
651 | 657 |
|
652 | 658 | /**
|
653 |
| - * Update one document |
654 |
| - * NOTE: Will update at most ONE document matching $filter |
| 659 | + * Updates at most one document matching the filter. |
655 | 660 | *
|
| 661 | + * @see ReplaceOne::__construct() for supported options |
656 | 662 | * @see http://docs.mongodb.org/manual/reference/command/update/
|
657 |
| - * @see Collection::getWriteOptions() for supported $options |
658 |
| - * |
659 |
| - * @param array $filter The document to be replaced |
660 |
| - * @param array $update An array of update operators to apply to the document |
661 |
| - * @param array $options Additional options |
| 663 | + * @param array|object $filter Query by which to filter documents |
| 664 | + * @param array|object $replacement Update to apply to the matched document |
| 665 | + * @param array $options Command options |
662 | 666 | * @return UpdateResult
|
663 | 667 | */
|
664 |
| - public function updateOne(array $filter, array $update, array $options = array()) |
| 668 | + public function updateOne($filter, $update, array $options = array()) |
665 | 669 | {
|
666 |
| - $firstKey = key($update); |
667 |
| - if (!isset($firstKey[0]) || $firstKey[0] != '$') { |
668 |
| - throw new InvalidArgumentException("First key in \$update must be a \$operator"); |
| 670 | + if ( ! isset($options['writeConcern']) && isset($this->wc)) { |
| 671 | + $options['writeConcern'] = $this->wc; |
669 | 672 | }
|
670 |
| - $wr = $this->_update($filter, $update, $options + array("multi" => false)); |
671 | 673 |
|
672 |
| - return new UpdateResult($wr); |
673 |
| - } |
674 |
| - |
675 |
| - /** |
676 |
| - * Internal helper for replacing/updating one/many documents |
677 |
| - * @internal |
678 |
| - */ |
679 |
| - protected function _update($filter, $update, $options) |
680 |
| - { |
681 |
| - $options = array_merge($this->getWriteOptions(), $options); |
| 674 | + $operation = new UpdateOne($this->dbname, $this->collname, $filter, $update, $options); |
| 675 | + $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); |
682 | 676 |
|
683 |
| - $bulk = new BulkWrite($options["ordered"]); |
684 |
| - $bulk->update($filter, $update, $options); |
685 |
| - return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc); |
| 677 | + return $operation->execute($server); |
686 | 678 | }
|
687 | 679 | }
|
0 commit comments