From 3c44d99baffd567d84cbbff31b79a0aa6a890cb8 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 21 Jul 2025 13:11:03 +0300 Subject: [PATCH] sync against feat-mongo-2 --- composer.json | 2 +- src/Client.php | 85 +++++++++++++++++++-------------------------- tests/MongoTest.php | 7 ++-- 3 files changed, 40 insertions(+), 54 deletions(-) diff --git a/composer.json b/composer.json index caf216322b..1ee3dcb499 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require": { "php": ">=8.0", "ext-mongodb": "*", - "mongodb/mongodb": "^1.21" + "mongodb/mongodb": "2.1.0" }, "require-dev": { "fakerphp/faker": "^1.14", diff --git a/src/Client.php b/src/Client.php index 511c01211b..8519d55312 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,6 @@ use MongoDB\BSON; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; -use MongoDB\BSON\Int64; class Client { @@ -144,7 +143,6 @@ class Client $sections = BSON\fromPHP($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; - return $this->send($message); } @@ -231,6 +229,7 @@ class Client if (property_exists($result, "nonce") && $result->ok === 1.0) { return $result; } + if ($result->ok === 1.0) { return $result; } @@ -520,7 +519,6 @@ class Client } $cleanUpdates[$k] = $v; } - $this->query( array_merge([ @@ -540,18 +538,20 @@ class Client } /** - * Perform multiple upserts in a single update command (wire protocol batch). - * Each operation should have 'filter' and 'update' keys. + * Insert, or update, document(s) with support for bulk operations. + * https://docs.mongodb.com/manual/reference/command/update/#syntax * * @param string $collection - * @param array $operations + * @param array $operations Array of operations, each with 'filter' and 'update' keys * @param array $options + * * @return self * @throws Exception */ - public function bulkUpsert(string $collection, array $operations, array $options = []): self + public function upsert(string $collection, array $operations, array $options = []): self { $updates = []; + foreach ($operations as $op) { $cleanUpdate = []; foreach ($op['update'] as $k => $v) { @@ -560,12 +560,16 @@ class Client } } - $updates[] = [ + $updateOperation = [ 'q' => $op['filter'], 'u' => $cleanUpdate, 'upsert' => true, + 'multi' => isset($op['multi']) ? $op['multi'] : false, ]; + + $updates[] = $updateOperation; } + $this->query( array_merge( [ @@ -578,48 +582,7 @@ class Client return $this; } - /** - * Insert, or update, a document/s. - * https://docs.mongodb.com/manual/reference/command/update/#syntax - * - * @param string $collection - * @param array $where - * @param array $updates - * @param array $options - * - * @return Client - * @throws Exception - */ - public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): self - { - $cleanUpdates = []; - - foreach ($updates as $k => $v) { - if (\is_null($v)) { - continue; - } - $cleanUpdates[$k] = $v; - } - - - $this->query( - array_merge( - [ - 'update' => $collection, - 'updates' => [ - [ - 'q' => ['_uid' => $where['_uid']], - 'u' => ['$set' => $cleanUpdates], - ] - ], - ], - $options - ) - ); - - return $this; - } /** * Find a document/s. @@ -924,4 +887,26 @@ class Client return $cleanedFilters; } -} \ No newline at end of file + private ?bool $replicaSet = null; + + /** + * Check if MongoDB is running as a replica set. + * + * @return bool True if this is a replica set, false if standalone + * @throws Exception + */ + public function isReplicaSet(): bool + { + if ($this->replicaSet !== null) { + return $this->replicaSet; + } + + $result = $this->query([ + 'isMaster' => 1, + ], 'admin'); + + $this->replicaSet = property_exists($result, 'setName'); + return $this->replicaSet; + } + +} diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 0bd87af4a0..56f2c5879c 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -110,6 +110,7 @@ class MongoTest extends TestCase public function testCreateDocuments(): array { + $docs = $this->getDatabase()->insertMany( 'movies', [ @@ -226,7 +227,7 @@ class MongoTest extends TestCase } - public function testBulkUpsert() + public function testUpsert() { $this->getDatabase()->insert( 'movies_upsert', @@ -238,7 +239,7 @@ class MongoTest extends TestCase ] ); - $this->getDatabase()->bulkUpsert('movies_upsert', [ + $this->getDatabase()->upsert('movies_upsert', [ [ 'filter' => ['name' => 'Gone with the wind'], 'update' => [ @@ -254,7 +255,6 @@ class MongoTest extends TestCase ], ]); - $documents = $this->getDatabase()->find('movies_upsert')->cursor->firstBatch ?? []; self::assertCount(2, $documents); self::assertEquals(4, $documents[0]->counter); @@ -262,4 +262,5 @@ class MongoTest extends TestCase self::assertEquals('USA', $documents[1]->country); self::assertEquals('English', $documents[1]->language); } + }