Updating documents in MongoDB with PHP involves using the updateOne
or updateMany
methods from the MongoDB PHP library. These methods allow for precise updates, whether targeting a single document or multiple ones.
Table of Content
Updating a Single Document
The updateOne
method is ideal for updating a single document. Specify a filter to identify the document and an update array to define changes.
$filter = ['username' => 'john_doe'];
$update = ['$set' => ['email' => '[email protected]']];
$result = $collection->updateOne($filter, $update);
echo $result->getModifiedCount() . " document updated.";
The filter targets documents with a specific username, and the $set
operator modifies the email field.
Let’s move on to the following section on how to update multiple documents at once.
Updating Multiple Documents
When multiple documents need updates, use updateMany
. This method applies changes to all matching documents.
$filter = ['status' => 'inactive'];
$update = ['$set' => ['status' => 'active']];
$result = $collection->updateMany($filter, $update);
echo $result->getModifiedCount() . " documents updated.";
This example updates the status
field for all documents marked as inactive. Let’s see how we can do that using additional operators in the section below.
Using Additional Update Operators
MongoDB supports various update operators, such as $inc
, $push
, and $unset
. These operators enable advanced update operations.
Incrementing a value
$filter = ['views' => ['$exists' => true]];
$update = ['$inc' => ['views' => 1]];
$result = $collection->updateOne($filter, $update);
echo $result->getModifiedCount() . " document updated.";
It increments the views
field for a document in a MongoDB collection. The $filter
checks if the views
field exists using the $exists
operator. The $update
uses the $inc
operator to increase the views
value by 1. The updateOne
method applies the update to the first matching document, and the getModifiedCount()
method returns the number of documents modified, which is then displayed.
Adding elements to an array
$filter = ['username' => 'john_doe'];
$update = ['$push' => ['tags' => 'new_tag']];
$result = $collection->updateOne($filter, $update);
echo $result->getModifiedCount() . " document updated.";
It adds a new value to the tags
array field in a MongoDB document. The $filter
targets a document where the username
is john_doe
. The $update
uses the $push
operator to append 'new_tag'
to the tags
array. The updateOne
method applies the update to the first matching document, and the getModifiedCount()
method outputs the number of documents modified.
Let’s summarize it.
Wrapping Up
Updating documents in MongoDB allows you to modify data in a simple way, So if you need to adjust single fields or update multiple records at once. It is an important for managing dynamic databases.
Here is a quick recap of what we covered:
- Updating a single document: Using
updateOne
with a filter and update array to target and modify specific fields. - Updating multiple documents: Using
updateMany
to apply changes to all matching documents. - Using additional operators: Employing
$inc
,$push
, and other operators for advanced update operations.
Similar Reads
Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…
The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…
The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…
The PHP switch statement is multiple conditions to only execute one block if its condition generating a true boolean value.…
PHP gives us the ability to work with MongoDB, highlighting the importance of CRUD (Create, Read, Update, Delete) operations for…
The filter_input_array() filters multiple inputs in PHP at once. It helps you to clean and validate data. Understand the filter_input_array…
JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…
In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…
Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…
The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…