Lesson 4: Updating Documents in PHP Applications / Learn

Code Summary: Updating Documents in PHP Applications

Review the following code, which demonstrates how to update documents in MongoDB by using the PHP MongoDB library.

Update a Single Document

To update a single document that matches a query, call the updateOne() method on the collection object.

The updateOne() method has two required parameters: a filter document that matches the document to update, and an update document that specifies the modifications to apply. In this example, the filter is assigned to the $filter variable, and the update is assigned to the $addToBalance variable.

updateOne() returns a result. In this example, we use the result to print a confirmation of the number of documents that were updated.

We also print the target document before and after the update. This way, we can confirm that the specified modification has been applied.

Here's the code:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$client = new MongoDB\Client('<Atlas-connection-string>');

$accountsCollection = $client->bank->accounts;

$filter = ['_id' => new MongoDB\BSON\ObjectId('62d6e04ecab6d8e130497482')];

$addToBalance = [
    '$inc' => [
        'balance' => new MongoDB\BSON\Decimal128('100'),
    ]
];

var_dump($accountsCollection->findOne($filter));

$updateOneResult = $accountsCollection->updateOne($filter, $addToBalance);

printf("Matched %d document(s)\n", $updateOneResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateOneResult->getModifiedCount());

$updatedExample = $accountsCollection->findOne($filter);

var_dump($updatedExample);

Update Multiple Documents

To update all documents that match a query, call the updateMany() method on the collection object.

The updateMany() method has two required parameters: a filter document that matches the document to update, and an update document that specifies the modifications to apply. In this example, the filter is assigned to the $filter variable, and the update is assigned to the $setField variable. Note that in this update, we define a new field and set its initial value.

updateMany() returns a result. In this example, we use the result to print out a confirmation of the number of documents that matched and were modified by the operation. We also print out one sample document after the update. This way, we can confirm that the specified modification has been applied.

Here's the code:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$client = new MongoDB\Client('<Atlas-connection-string>');

$accountsCollection = $client->bank->accounts;

$filter = ['account_type' => 'savings'];

$setField = ['$set' => ['minimum_balance' => new MongoDB\BSON\Decimal128('100')]];

$updateManyResult = $accountsCollection->updateMany($filter, $setField);

printf("Matched %d document(s)\n", $updateManyResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateManyResult->getModifiedCount());

$updatedExample = $accountsCollection->findOne($filter);

var_dump($updatedExample);