How to Limit the Query Results in MongoDB using PHP
If you’re a PHP developer working with MongoDB, you know how easy it is to query the database from your PHP application. However, there may be certain situations where you want to limit the results you get from MongoDB– for example, you might want to get just a single result or a maximum of 10 results. Regardless of your specific requirements, it’s easy to make this happen with just a few lines of code. In this tutorial, we’ll talk about limiting MongoDB query results using PHP.
Before we dive into the PHP code needed to limit query results from MongoDB, it’s important to make sure certain prerequisites are in place. For this task, a few key system requirements include:
First, you need to ensure that both MongoDB and the MongoDB PHP driver are properly configured beforehand.
To check if the MongoDB driver is installed, use the following command in your terminal window:
`bash pecl search mongo
The result will look something like the following:
Package Stable/(Latest) Local
mongo 1.6.16 (stable) MongoDB database driver
mongodb 1.6.0alpha1 (alpha) 1.5.3 MongoDB driver for PHP
- To determine which version of PHP is installed on your system, use the command shown below:
- You’ll get a response that should look something like this:
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
- The compatibility chart shown below can help you select the correct MongoDB driver for your installed version of PHP:
Starting the MongoDB Daemon
Once you’ve confirmed that all the system requirements are in place, you can move on to starting the MongoDB daemon. First, open your terminal by pressing Ctrl + Alt + T. You can start the MongoDB service and check the status using the command shown below.
It’s a good idea to use the
sudo command when starting the service to avoid permission-related issues:
sudo systemctl start mongod
sudo systemctl status mongod
You should see output from that command that looks something like this:
How to Limit Query Results
Now that we’ve successfully started up MongoDB, it’s time to look at some PHP code. The two code snippets below allows you to make a query to MongoDB and limit the number of results that are returned. The first uses the MongoDB Driver Manager, and the second uses the PHP library for MongoDB:
// Using MongoDB\Driver\Manager
$filter = ;
$options = ['limit' => 1];
$query = new MongoDB\Driver\Query($filter, $options);
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$result = $manager->executeQuery('pastTimeDb.movie', $query);
// Using PHP Library
$collection = (new MongoDB\Client)->pastTimeDB->movie;
$result = $collection->find(array(),array('limit' => 1));
Both scripts shown above would typically return batches of results; however, note that the query results were limited to return only one (1) result due to the parameter
limit => 1. In the first script, we defined the limit in the variable
options, and then we passed the options into the
query. In the second script, the limit was simply added as an option as part of the
NOTE: the value of
limit defines the maximum number of documents to be returned. In our example, we used a value of 1, but you can specify any value you want.
When you query MongoDB from a PHP script, there are times when you might not want the entire results set returned to you. In these situations, it’s easy to limit the number of results that are returned. All it takes is a simple bit of code to specify the maximum number of results you’d like to have returned from your query. With the instructions provided in this article, you should have no trouble limiting MongoDB query results using PHP.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started