Skip to content

Commit

Permalink
Fix bulkUpdate and bulkInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Apr 6, 2024
1 parent 7ab917d commit a73723f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 32 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"php": ">=7.4",
"ext-curl": "*",
"aws/aws-sdk-php": "3.*",
"byjg/anydataset": "4.9.*",
"byjg/anydataset": "4.9.x-dev",
"byjg/anydataset-array": "4.9.*",
"byjg/serializer": "4.9.*",
"byjg/webrequest": "4.9.*",
Expand Down
87 changes: 56 additions & 31 deletions src/MongoDbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function getDocuments(IteratorFilter $filter, $collection = null)

$dataCursor = $this->mongoManager->executeQuery(
$this->database . '.' . $collection,
$this->getMongoFilterArray($filter)
new Query($this->getMongoFilterArray($filter))
);

if (empty($dataCursor)) {
Expand All @@ -168,10 +168,44 @@ public function getDocuments(IteratorFilter $filter, $collection = null)
return $result;
}

protected function getMongoFilterArray(IteratorFilter $filter)
protected function getMongoFilterArray(IteratorFilter $filter): array
{
$result = [];

$data = [
Relation::EQUAL => function ($value) {
return $value;
},
Relation::GREATER_THAN => function ($value) {
return [ '$gt' => $value ];
},
Relation::LESS_THAN => function ($value) {
return [ '$lt' => $value ];
},
Relation::GREATER_OR_EQUAL_THAN => function ($value) {
return [ '$gte' => $value ];
},
Relation::LESS_OR_EQUAL_THAN => function ($value) {
return [ '$lte' => $value ];
},
Relation::NOT_EQUAL => function ($value) {
return [ '$ne' => $value ];
},
Relation::STARTS_WITH => function ($value) {
return [ '$regex' => "^$value" ];
},
Relation::CONTAINS => function ($value) {
return [ '$regex' => "$value" ];
},
Relation::IN => function ($value) {
return [ '$in' => $value ];
},
Relation::NOT_IN => function ($value) {
return [ '$nin' => $value ];
},
];


foreach ($filter->getRawFilters() as $itemFilter) {
$name = $itemFilter[1];
$relation = $itemFilter[2];
Expand All @@ -185,37 +219,10 @@ protected function getMongoFilterArray(IteratorFilter $filter)
throw new InvalidArgumentException('MongoDBDriver does not support filtering the same field twice');
}

$data = [
Relation::EQUAL => function ($value) {
return $value;
},
Relation::GREATER_THAN => function ($value) {
return [ '$gt' => $value ];
},
Relation::LESS_THAN => function ($value) {
return [ '$lt' => $value ];
},
Relation::GREATER_OR_EQUAL_THAN => function ($value) {
return [ '$gte' => $value ];
},
Relation::LESS_OR_EQUAL_THAN => function ($value) {
return [ '$lte' => $value ];
},
Relation::NOT_EQUAL => function ($value) {
return [ '$ne' => $value ];
},
Relation::STARTS_WITH => function ($value) {
return [ '$regex' => "^$value" ];
},
Relation::CONTAINS => function ($value) {
return [ '$regex' => "$value" ];
},
];

$result[$name] = $data[$relation]($value);
}

return new Query($result);
return $result;
}

public function deleteDocumentById($idDocument, $collection = null)
Expand All @@ -233,9 +240,27 @@ public function deleteDocuments(IteratorFilter $filter, $collection = null)
}

$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);

$query = $this->getMongoFilterArray($filter);
$bulkWrite = new BulkWrite();
$bulkWrite->delete($query);
$this->mongoManager->executeBulkWrite(
$this->database . '.' . $collection,
$bulkWrite,
$writeConcern
);
}

$bulkWrite->delete($this->getMongoFilterArray($filter));
public function updateDocuments(IteratorFilter $filter, $data, $collection = null)
{
if (empty($collection)) {
throw new InvalidArgumentException('Collection is mandatory for MongoDB');
}

$query = $this->getMongoFilterArray($filter);
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 100);
$bulkWrite = new BulkWrite();
$bulkWrite->update($query, ["\$set" => $data], ["multi" => 1]);
$this->mongoManager->executeBulkWrite(
$this->database . '.' . $collection,
$bulkWrite,
Expand Down
8 changes: 8 additions & 0 deletions src/NoSqlInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function deleteDocumentById($idDocument, $collection = null);
*/
public function deleteDocuments(IteratorFilter $filter, $collection = null);

/**
* @param IteratorFilter $filter
* @param array $data
* @param null $collection
* @return mixed
*/
public function updateDocuments(IteratorFilter $filter, $data, $collection = null);

/**
* @return mixed
*/
Expand Down
41 changes: 41 additions & 0 deletions tests/MongoDbDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public function testDelete()
// Check if object do not exist
$document = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION);
$this->assertEmpty($document);

// Check if other objects weren't deleted
$filter = new IteratorFilter();
$filter->addRelation('name', Relation::EQUAL, 'Hilux');
$document = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION);
$this->assertCount(1, $document);
}

/**
Expand Down Expand Up @@ -288,4 +294,39 @@ public function testGetDocuments()
$this->assertEquals('Chevrolet', $documents[0]->getDocument()['brand']);
$this->assertEquals('60000', $documents[0]->getDocument()['price']);
}

/**
* @throws \MongoDB\Driver\Exception\Exception
*/
public function testBulkUpdate()
{
if (empty($this->dbDriver)) {
$this->markTestIncomplete("In order to test MongoDB you must define MONGODB_CONNECTION");
}

// Get the Object to test
$filter = new IteratorFilter();
$filter->addRelation('brand', Relation::IN, ['Toyota', 'Audi']);
$documentList = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION);
$this->assertCount(3, $documentList);
foreach ($documentList as $document) {
$this->assertNotEquals('30000', $document->getDocument()['price']);
}

// Delete
$this->dbDriver->updateDocuments($filter, ["price" => 30000], self::TEST_COLLECTION);

// Check if object do not exist
$documentList = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION);
$this->assertCount(3, $documentList);
foreach ($documentList as $document) {
$this->assertEquals('30000', $document->getDocument()['price']);
}

// Check if other objects weren't deleted
$filter = new IteratorFilter();
$filter->addRelation('name', Relation::EQUAL, 'Hilux');
$documentList = $this->dbDriver->getDocuments($filter, self::TEST_COLLECTION);
$this->assertCount(1, $documentList);
}
}

0 comments on commit a73723f

Please sign in to comment.