How to Update MongoDB Document using PHP

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

MongoDB is an open-source database developed by MongoDB that stores data as documents in a binary representation, or Binary JSON, called BSON. The data objects are stored as individual documents inside a collection of documents. As such, updating MongoDB Document using PHP can be done one at a time or in groups. You will need to confirm that you have the proper version of both the MongoDB driver and PHP, as not all version are compatible with each other.

  • The process of updating MongoDB Document is fairly simple, however, it requires properly configured compatible versions of PHP and MongoDB driver.

Prerequisites

  • Confirm that MongoDB and MongoDB PHP drivers are properly configured before you begin.

  • You can quickly determine if you have MongoDB driver installed by executing the following command:

1
pecl search mongo
  • below is the result
1
2
3
Package Stable/(Latest) Local
mongo 1.6.16 (stable) MongoDB database driver
mongodb 1.6.0alpha1 (alpha) 1.5.3 MongoDB driver for PHP
  • You can execute the following command to see what version of PHP is installed on your system:
1
php --version
  • The result should look similar to this:
1
2
3
4
PHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.15-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
  • Refer to the below chart for mongodb driver compatibility applications issues.

table mongodb php driver

Begin by Starting the MongoDB Daemon

  • First, open your terminal by pressing Ctrl + Alt + T
  • Then start the MongoDB service and confirm the status using the below command.
  • You should use sudo command to avoid permission-related issues while starting MongoDB service.
1
2
sudo systemctl start mongod
sudo systemctl status mongod
  • You should see something like this.

starting mongod service

How to Update Only Specific Fields of a MongoDB Document Using PHP

  • The following example shows you how to update documents with the movie_name of “God Father” by setting the genre field to “suspense”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Using MongoDBDriverManager

<?php

$bulk = new MongoDBDriverBulkWrite;
$bulk->update(
['movie_name' => "God Father"],
['$set' => ['genre' => "suspense"]],
);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('pastTimeDb.movie', $bulk);

?>

// Using PHP Library

<?php

$collection = (new MongoDBClient)->pastTimeDB->movie;

$updateResult = $collection->updateOne(
[ 'movie_name' => 'God Father' ],
[ '$set' => [ 'genre' => 'suspense' ]]
);

?>

How to Update a Single MongoDB Document Using PHP

  • The MongoDBDeriverManager script will update the first document matching the criteria even if there is a multiple-criteria match. This is because of the multi parameter being set to false.

  • By default, the PHP Library updateOne() method will update one document that matches the criteria, and only the first matching document will be updated even if multiple matches occur.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Using MongoDBDriverManager

$updateRec = new MongoDBDriverBulkWrite;
$bulk->update(
['movie_name' => "God Father"],
['$set' => ['genre' => "suspense"]],
['multi' => false]
);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('globe_bank.subjects', $updateRec);


// Using PHP Library
$collection = (new MongoDBClient)->pastTimeDB->pastTimeDB;
$updateResult = $collection->updateOne(
[ 'movie_name' => 'God Father' ],
[ '$set' => [ 'genre' => 'suspense' ]]
);

How to Update Multiple MongoDB Documents Using PHP

  • The MongoDBDeriverManager script will update documents that matches the specified criteria when the multi parameter is set to true.

  • The PHP Library updateMany() method will update all the documents that matches the specified criteria.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Using MongoDBDriverManager
$updateRec = new MongoDBDriverBulkWrite;
$bulk->update(
['movie_name' => "God Father"],
['$set' => ['genre' => "suspense"]],
['multi' => True]
);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('globe_bank.subjects', $updateRec);



// Using PHP Library
$collection = (new MongoDBClient)->pastTimeDB->pastTimeDB;
$updateResult = $collection->updateMany(
[ 'movie_name' => 'God Father' ],
[ '$set' => [ 'genre' => 'suspense' ]]
);

Conclusion

The motivation behind the MongoDB language is allow you to implement a data store that offers high performance, availability and automatic scaling. In this tutorial you learned the process of updating MongoDB Document using PHP and the different commands for updating single and multiple MongoDB Documents. It is crucial to remember to use sudo command to avoid permission-related issues while starting the MongoDB service as well as confirm there are no PHP and MongoDB driver compatibility-applications issues.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.