Docs Menu
Docs Home
/ / /
PHP Library Manual

Read Data from MongoDB

On this page

  • Overview
  • Sample Application
  • Find One
  • Find Multiple
  • Count Documents in a Collection
  • Count Documents Returned from a Query
  • Estimated Document Count
  • Retrieve Distinct Values
  • Monitor Data Changes

On this page, you can see copyable code examples that show common PHP library methods for retrieving documents.

Tip

To learn more about any of the methods shown on this page, see the link provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Make sure to set the MONGODB_URI environment variable to the connection string for your MongoDB deployment, and replace the <database> and <collection> placeholders with values for your target namespace.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the MongoDB PHP Library installed in your project. To learn more about installing the MongoDB PHP Library, see the Download and Install guide.

  2. Copy the following code and paste it into a new .php file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1<?php
2
3require __DIR__ . '/../vendor/autoload.php';
4
5$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6$client = new MongoDB\Client($uri);
7
8$collection = $client->selectCollection('<database>', '<collection>');
9
10// Start example code here
11
12// End example code here

The following code shows how to retrieve a single document from a collection that matches the specified criteria:

$document = $collection->findOne(['year' => 1994]);
echo json_encode($document), PHP_EOL;

To learn more about the findOne() method, see the Find One Document section in the Retrieve Data guide.

The following code shows how to retrieve all documents from a collection that match the specified criteria:

$resultsMultiple = $collection->find(['year' => 1970]);
foreach ($resultsMultiple as $doc) {
echo json_encode($doc), PHP_EOL;
}

To learn more about the find() method, see the Find Multiple Documents section in the Retrieve Data guide.

The following code shows how to count the number of documents in a collection:

$result = $collection->countDocuments([]);
echo 'Number of documents: ', $result;

To learn more about the countDocuments() method, see the Count All Documents section in the Count Documents guide.

The following code shows how to count documents in a collection that match the specified criteria:

$result = $collection->countDocuments(['year' => 2010]);
echo 'Number of companies founded in 2010: ', $result;

To learn more about the countDocuments() method, see the Count Specific Documents section in the Count Documents guide.

The following code shows how to retrieve an estimated count of the number of documents in a collection:

$result = $collection->estimatedDocumentCount();
echo 'Estimated number of documents: ', $result;

To learn more about the estimatedDocumentCount() method, see the Retrieve an Estimated Count section in the Count Documents guide.

The following code shows how to retrieve the unique values of a field for documents that match the specified criteria:

$results = $collection->distinct('year');
foreach ($results as $value) {
echo json_encode($value), PHP_EOL;
}

To learn more about the distinct() method, see the Retrieve Distinct Field Values guide.

The following code shows how to monitor and print changes to a collection:

$changeStream = $collection->watch();
for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
echo toJSON($event), PHP_EOL;
if ($event['operationType'] === 'invalidate') {
break;
}
}

To learn more about the watch() method, see the Monitor Data Changes guide.

Back

Time Series Collections