From f59a7641af0d09bcee4e17cad6533dc625655a99 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 16 Mar 2023 23:28:59 +1300 Subject: [PATCH 1/2] Add multi param to update to allow single or multiple updates --- src/Adapter.php | 15 --------------- src/Client.php | 5 +++-- 2 files changed, 3 insertions(+), 17 deletions(-) delete mode 100644 src/Adapter.php diff --git a/src/Adapter.php b/src/Adapter.php deleted file mode 100644 index 521b01ca51..0000000000 --- a/src/Adapter.php +++ /dev/null @@ -1,15 +0,0 @@ - $this->toObject($where), 'u' => $this->toObject($cleanUpdates), - 'multi' => false, + 'multi' => $multi, 'upsert' => false ] ] From 7ac9a8abb8b968ba0b5b8dbe57513aa1ddbeb7b6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 16 Mar 2023 23:46:23 +1300 Subject: [PATCH 2/2] Add tests --- tests/MongoTest.php | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index ab67e41a9b..e2760a5647 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -108,6 +108,53 @@ class MongoTest extends TestCase self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back } + public function testUpdateDocument(): void + { + $this->getDatabase()->insert( + 'movies', + [ + 'name' => 'Armageddon', + 'country' => 'AUS', + 'language' => 'English' + ] + ); + + $this->getDatabase()->update( + 'movies', + ['name' => 'Armageddon'], + ['$set' => ['name' => 'Armageddon 2']] + ); + + $doc = $this->getDatabase()->find( + 'movies', + ['name' => 'Armageddon 2'] + )->cursor->firstBatch ?? []; + + self::assertCount(1, $doc); + } + + public function testUpdateMultipleDocuments(): void + { + $this->getDatabase()->update( + 'movies', + ['name' => 'Armageddon 2'], + ['$set' => ['name' => 'Armageddon']] + ); + + $this->getDatabase()->update( + 'movies', + ['name' => 'Armageddon'], + ['$rename' => ['name' => 'title']], + multi: true + ); + + $docs = $this->getDatabase()->find( + 'movies', + ['title' => 'Armageddon'] + )->cursor->firstBatch ?? []; + self::assertCount(2, $docs); + } + public function testDriverException() { self::expectException('MongoDB\Driver\Exception\InvalidArgumentException');