How to Retrieve MongoDB Document using PHP

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

How to Retrieve MongoDB Document using PHP

Introduction

When you use PHP to locate documents in MongoDB based on specific criteria, if the documents are there, you’ll find them. It’s important to know though, that you must have compatible versions of Mongo DB and PHP in order to locate the documents you want. What’s more, MongoDB and PHP must be properly configured as well. This tutorial can help you with that. Review these steps on retrieving MongoDB document using PHP, and you’ll sail through mastering the art of document retrieval in no time at all.

Prerequisites

  • Install MongoDB and run it.

  • Match the MongoDB PHP Driver with your version of MongoDB.

  • Use the pecl command below to check that your MongoDB driver is installed:

1
pecl search mongo
  • Here’s a successful result. You should see something similar to it.
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
  • Use the command below to verify your PHP version:
1
php --version
  • The outcome should resemble 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
  • Use this reference table for you to match your PHP driver with a compatible MongoDB version.

table mongodb php driver

Start and check the status of MongoDB

  • Press Ctrl + Alt + T to open a new terminal window.

  • Use the two sudo commands to start MongoDB and check the status. See below:

1
2
sudo systemctl start mongod
sudo systemctl status mongod
  • This result shows the service actively running. You should see likewise:

starting mongod service

Use PHP to Find a Mongo Document

  • Here’s an example of a script that finds a document in the database named pastTimeDB from the collection named movie.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Using MongoDBDriverManager
$filter = [];
$options = [];
$query = new MongoDBDriverQuery($filter, $options);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeQuery('pastTimeDb.movie', $query);


// Using PHP Library


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

Use PHP to Find Just One Document

  • See the example below that uses PHP to find just one document in the pastTimeDB database from the movie collection in MongoDB.

  • Note that the $filter is guided by whatever parameter is set in $options. In this example, the limit => 1 is the parameter in the script tells it to locate one document.

  • The findOne() is a default setting in the PHP Library that finds only one document. It’s based on the query.

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


$filter = ['movie_name' => 'God Father'];
$options = ['limit' => 1];
$query = new MongoDBDriverQuery($filter, $options);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeQuery('pastTimeDb.movie', $query);


// Using PHP Library


$collection = (new MongoDBClient)->pastTimeDB->movie;
$query = ['movie_name' => 'God Father' ];
$updateResult = $collection->findOne($query);

Use PHP “”Using MongoDBDriverManager”” to Locate MongoDB Documents

  • Whatever the parameter $filter specifies is what the PHP script ""Using MongoDBDriverManager"" will go by. It finds those documents based on the $filter.

  • Again, the find() function is a default in the PHP Library. It adheres to whatever the query specifies.

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


$filter = ['movie_name' => 'God Father'];
$options = [];
$query = new MongoDBDriverQuery($filter, $options);
$manager = new MongoDBDriverManager('mongodb://localhost:27017');
$result = $manager->executeQuery('pastTimeDb.movie', $query);


// Using PHP Library


$collection = (new MongoDBClient)->pastTimeDB->movie;
$query = ['movie_name' => 'God Father' ];
$updateResult = $collection->find($query);

Conclusion

This tutorial showed you the way of retrieving MongoDB document using PHP. You learned how to locate a document using the PHP Library’s find() function which has a default setting of finding just one document. We also went over some script examples that included ""Using MongoDBDriverManager"" to locate one or more documents. The role that $filters play in search is vital to narrow down the details about the data you need to retrieve. Now that you know how to use PHP to retrieve a document in MongoDB, let it become second nature for you to use in all of your coding projects.

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.