How to Manipulate MongoDB fields using PHP

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

Introduction

Your MongoDB document database is a repository that adapts to change. It reacts to “change” by being responsive to how you want to find the data that it houses for you. When you have more control over filtering that data, the better you can find the documents you need to analyze. The main thing about it is that it’s extremely helpful to be able to manipulate fields and search for specific criteria at any time. This is because what you’ll need to locate will likely differ from day-to-day. It’s all about meeting your index search requirements so you can quickly shape the data results. Find out the best way to do that with this tutorial on manipulating MongoDB fields using PHP.

Prerequisites

  • MongoDB – Download the stable, current version of MongoDB. Install it, then run the service.

  • MongoDB PHP Driver – Get the appropriate version (the one that is compatible) to go with your MongoDB.

  • The pecl command below lets you know if your MongoDB driver is installed. Use it like this:

1
pecl search mongo
  • A good result is what you should see. Here’s one:
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
  • This command tells you what version of PHP is on your system. See below:
1
php --version
  • Here’s what you should expect to see:
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
  • This compatibility table is a handy reference. It shows PHP Drivers and the MongoDB versions that match them.

table mongodb php driver

MongoDB: Time to Start it Up

  • Access a terminal window by pressing Ctrl + Alt + T.

  • The best way to avoid permission issues is to use a sudo command. Here’s the two you’ll want to use to start MongoDB service and check its status.

1
2
sudo systemctl start mongod
sudo systemctl status mongod
  • The MongoDB service has loaded. The Active status is highlighted green and is “active (running)”. It also gives the exact date down to the seconds it has been active. It’s in real time. An example of a similar result you might see is here:

starting mongod service

Use the $set Operator to Manipulate MongoDB Fields

  • With the $set operator, you can manipulate a field with a value that you specify.
1
2
3
4
5
6
7
8
9
10
// Using MongoDB\Driver\Manager

$updateRec = new MongoDB\Driver\BulkWrite;
$updateRec->update(
['_id'=>new MongoDB\BSON\ObjectID($id)], // (1)
['$set' => [ // (2)
'brand' => 'apple',
'model'=> '5s']
]
);

(1) The variable $id filters out the id documents that match it and returns them in the results.

(2) Here’s where $set comes in. In the above example, both the brand field and the model field can be updated with different values that you determine.

  • A PHP Library script using the $set example is shown here:
1
2
3
4
5
6
7
8
9
10
11
12
// Using PHP Library

$db = $con->abi_db;
$collection = $db->subjects;
$updateResult = $collection->updateOne(
[`brand` => 'apple'],
['$set' => [
'model'=> '5s'
]
],
['multi' => false, 'upsert' => false]
);

Use the $unset Operator to Manipulate MongoDB Fields

  • You can remove a certain MongoDB field with the $unset operator.

  • Here’s an example of the $unset operator in action. See below:

1
2
3
4
5
6
7
8
9
// Using MongoDB Driver Manager
$updateRec = new MongoDB\Driver\BulkWrite;
$updateRec->update(
[`brand` => 'apple'],
['$unset' => [
'model'=> '5s'
],
['multi' => false, 'upsert' => false]
);

Conclusion

This tutorial focused on manipulating MongoDB fields using PHP. You learned how to use the $set operator to change one or more data fields in a PHP script in MongoDB. You also found out how to remove a particular MongoDB field using the $unset operator. Both of these techniques are useful in modifying data fields to return relevant results. Test out these manipulation methods today and you’ll be readily prepared to rise to the data field challenge of change.

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.