PHP & MongoDB - Updating Documents



Steps to Update a Document in a collection

First step to do any operation is to create a MongoDB Client instance.

$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);

Second step is to prepare and execute a filter to select a document and update document using updateOne() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define a Filter
$filter = ['e_mail' => 'mahesh_parashar.123@gmail.com'];

$update = ['$set' => ['e_mail' => 'mahesh_parashar@gmail.com']];
$options = [];

// update the document
$updatedResult = $collection->updateOne($filter, $update, $options);

Example - Updating a Document of a MongoDB Collection

Try the following example to update a single document of a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Filter
   $filter = ['e_mail' => 'mahesh_parashar.123@gmail.com'];

   $update = ['$set' => ['e_mail' => 'mahesh_parashar@gmail.com']];
   $options = [];

   // update the document
   $updatedResult = $collection->updateOne($filter, $update, $options);

   printf("Matched %d document.<br/>", $updatedResult->getMatchedCount());
   printf("Modified %d document.", $updatedResult->getModifiedCount());
?>

Output

Access the index.php deployed on apache web server and verify the output.

Matched 1 document.
Modified 1 document.

Steps to Update Multiple Documents from a collection

Prepare and execute a command to update a collection and update documents using updateMany() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define a Filter
$filter = ['phone' => '9000012345'];

$update = ['$set' => [phone => '90000123451']];
$options = [];

// update the documents
$updatedResult = $collection->updateMany($filter, $update, $options);

Example - Updating Multiple Documents of a MongoDB Collection

Try the following example to update multiple documents of a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Filter
   $filter = ['phone' => '9000012345'];

   $update = ['$set' => [phone => '90000123451']];
   $options = [];

   // update the document
   $updatedResult = $collection->updateMany($filter, $update, $options);

   printf("Matched %d document(s).<br/>", $updatedResult->getMatchedCount());
   printf("Modified %d document(s).", $updatedResult->getModifiedCount())
?>

Output

Access the index.php deployed on apache web server and verify the output.

Matched 2 document(s).
Modified 2 document(s).
Advertisements