diff --git a/src/Client.php b/src/Client.php index 74ba939145..547b7515a3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -141,7 +141,7 @@ class Client { return Uuid::uuid7()->toString(); } - + /** * Send a raw string query to connection. * @param string $qry @@ -476,7 +476,9 @@ class Client $docObj->{$key} = $value; } - $docObj->_id ??= $this->createUuid(); + if (!isset($docObj->_id) || $docObj->_id === '' || $docObj->_id === null) { + $docObj->_id = $this->createUuid(); + } $this->query(array_merge([ self::COMMAND_INSERT => $collection, @@ -497,7 +499,9 @@ class Client $docObj->{$key} = $value; } - $docObj->_id ??= $this->createUuid(); + if (!isset($docObj->_id) || $docObj->_id === '' || $docObj->_id === null) { + $docObj->_id = $this->createUuid(); + } $docObjs[] = $docObj; } diff --git a/tests/MongoTest.php b/tests/MongoTest.php index aba9be3653..d0fe69898c 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -62,17 +62,17 @@ class MongoTest extends TestCase ); $id = (string)$doc['_id']; - self::assertEquals(24, strlen($id)); + self::assertEquals(36, strlen($id)); $doc = $this->getDatabase()->find('movies', ['name' => 'Armageddon'])->cursor->firstBatch ?? []; self::assertCount(1, $doc); - $doc = $this->getDatabase()->find('movies', ['_id' => new ObjectId($id)])->cursor->firstBatch ?? []; + $doc = $this->getDatabase()->find('movies', ['_id' => $id])->cursor->firstBatch ?? []; self::assertCount(1, $doc); $doc = $this->getDatabase()->insert('movies', ['9 Monkeys']); $id = (string)$doc['_id']; - self::assertEquals(24, strlen($id)); + self::assertEquals(36, strlen($id)); $doc = $this->getDatabase()->insert( 'movies',