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