Skip to content

Commit

Permalink
Merge pull request #13 from utopia-php/feat-insert-many
Browse files Browse the repository at this point in the history
Add insert many function
  • Loading branch information
abnegate authored Aug 14, 2023
2 parents b6dfb31 + 4ab1a0c commit d94717d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,34 @@ public function insert(string $collection, array $document, array $options = [])
return $this->toArray($docObj);
}

public function insertMany(string $collection, array $documents, array $options = []): array
{
$docObjs = [];

foreach ($documents as $document) {
$docObj = new stdClass();

foreach ($document as $key => $value) {
if (\is_null($value)) {
continue;
}

$docObj->{$key} = $value;
}

$docObj->_id ??= new BSON\ObjectId();

$docObjs[] = $docObj;
}

$this->query(array_merge([
self::COMMAND_INSERT => $collection,
'documents' => $docObjs,
], $options));

return $this->toArray($docObjs);
}

/**
* Retrieve the last lastDocument
*
Expand Down
56 changes: 50 additions & 6 deletions tests/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ public function testCreateDocument()
self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back
}

public function testCreateDocuments(): array
{
$docs = $this->getDatabase()->insertMany(
'movies',
[
[
'name' => 'Armageddon',
'country' => 'USA',
'language' => 'English'
],
[
'name' => '9 Monkeys',
'country' => 'USA',
'language' => 'English'
],
[
'name' => 300,
'country' => 'USA',
'language' => 'English'
]
]
);

self::assertCount(3, $docs);

return $docs;
}

public function testUpdateDocument(): void
{
$this->getDatabase()->insert(
Expand All @@ -133,26 +161,42 @@ public function testUpdateDocument(): void
self::assertCount(1, $doc);
}

public function testUpdateMultipleDocuments(): void
/**
* @depends testCreateDocuments
* @return void
* @throws Exception
*/
public function testUpdateDocuments(array $documents): void
{
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon 2'],
['$set' => ['name' => 'Armageddon']]
updates: ['$set' => ['name' => 'Armageddon 2']],
multi: true
);

$docs = $this->getDatabase()->find(
'movies',
filters: ['name' => 'Armageddon 2']
)->cursor->firstBatch ?? [];

self::assertCount(8, $docs);
}

public function testUpdateMultipleDocuments(): void
{
$this->getDatabase()->update(
'movies',
['name' => 'Armageddon'],
['name' => 'Armageddon 2'],
['$rename' => ['name' => 'title']],
multi: true
);

$docs = $this->getDatabase()->find(
'movies',
['title' => 'Armageddon']
['title' => 'Armageddon 2']
)->cursor->firstBatch ?? [];
self::assertCount(2, $docs);

self::assertCount(8, $docs);
}

public function testDriverException()
Expand Down

0 comments on commit d94717d

Please sign in to comment.