Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insert many function #13

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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