PHP & MongoDB Examples
- PHP & MongoDB - Connect Database
- PHP & MongoDB - Show Databases
- PHP & MongoDB - Drop Database
- PHP & MongoDB - Create Collection
- PHP & MongoDB - Drop Collection
- PHP & MongoDB - Display Collections
- PHP & MongoDB - Insert Document
- PHP & MongoDB - Select Document
- PHP & MongoDB - Update Document
- PHP & MongoDB - Delete Document
- PHP & MongoDB - Embedded Documents
- PHP & MongoDB - Error Handling
- PHP & MongoDB - Limiting Records
- PHP & MongoDB - Sorting Records
PHP & MongoDB Useful Resources
PHP & MongoDB - Selecting Documents
Steps to Select a Document in a collection
First step to do any operation is to create a MongoDB Client instance.
$uri = "mongodb://localhost:27017"; // connect to mongodb $client = new MongoDB\Client($uri);
Second step is to prepare and execute a filter to select a document and select document using findOne() command.
// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');
// Define a Filter
$filter = ['e_mail' => 'mahesh_parashar.123@gmail.com'];
// execute the query
$document = $collection->findOne($filter);
// Print result
if ($document) {
echo "Found document for Mahesh Parashar:"."<br/>";
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>"
} else {
echo "No document found for Mahesh Parashar."."<br/>";
}
Example - Selecting a Document from a MongoDB Collection
Try the following example to select a single document from a MongoDB collection −
index.php
<?php
require __DIR__ . '\vendor\autoload.php';
$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);
// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');
// Define a Filter
$filter = ['e_mail' => 'mahesh_parashar.123@gmail.com'];
// execute the query
$document = $collection->findOne($filter);
// Print result
if ($document) {
echo "Found document for Mahesh Parashar:"."<br/>";
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
} else {
echo "No document found for Mahesh Parashar."."<br/>";
}
?>
Output
Access the index.php deployed on apache web server and verify the output.
Found document for Mahesh Parashar:
{
"_id": {
"$oid": "696c20bffed319cd950058c4"
},
"First_Name": "Mahesh",
"Last_Name": "Parashar",
"Date_Of_Birth": "1990-08-21",
"e_mail": "mahesh_parashar.123@gmail.com",
"phone": "9034343345"
}
Steps to Select Multiple Documents from a collection
Prepare and execute a command to select a collection and select documents using find() command.
// Define the minimum length
$minLength = 6;
// Define the query filter for size of first name
$filter = [
'First_Name' => ['$exists' => true],
'$expr' => [
'$gt' => [
['$strLenCP' => '$First_Name'], $minLength
]
]
];
// execute the query
$cursor = $collection->find($filter);
// Iterate over the cursor to access each document
foreach ($cursor as $document) {
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
}
Example - Selecting Multiple Documents from a MongoDB Collection
Try the following example to select multiple documents from a MongoDB collection −
index.php
<?php
require __DIR__ . '\vendor\autoload.php';
$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);
// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');
// Define the minimum length
$minLength = 6;
// Define the query filter for size of first name
$filter = [
'First_Name' => ['$exists' => true],
'$expr' => [
'$gt' => [
['$strLenCP' => '$First_Name'], $minLength
]
]
];
// execute the query
$cursor = $collection->find($filter);
foreach ($cursor as $document) {
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
}
?>
Output
Access the index.php deployed on apache web server and verify the output.
{
"_id": {
"$oid": "696c230afed319cd950058c6"
},
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26",
"e_mail": "radhika_sharma.123@gmail.com",
"phone": "9000012345"
}
{
"_id": {
"$oid": "696c230afed319cd950058c8"
},
"First_Name": "Fathima",
"Last_Name": "Sheik",
"Date_Of_Birth": "1990-02-16",
"e_mail": "fathima_sheik.123@gmail.com",
"phone": "9000012345"
}
Steps to Select All Documents from a collection
Prepare and execute a command to select a collection and select all documents using find() command.
// execute the query
$cursor = $collection->find();
foreach ($cursor as $document) {
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
}
Example - Selecting All Documents from a MongoDB Collection
Try the following example to select all documents from a MongoDB collection −
index.php
<?php
require __DIR__ . '\vendor\autoload.php';
$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);
// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');
// execute the query
$cursor = $collection->find();
// Iterate over the cursor to access each document
foreach ($cursor as $document) {
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
}
?>
Output
Access the index.php deployed on apache web server and verify the output.
{
"_id": {
"$oid": "696c20bffed319cd950058c4"
},
"First_Name": "Mahesh",
"Last_Name": "Parashar",
"Date_Of_Birth": "1990-08-21",
"e_mail": "mahesh_parashar.123@gmail.com",
"phone": "9034343345"
}
{
"_id": {
"$oid": "696c230afed319cd950058c5"
},
"First_Name": "Mahesh",
"Last_Name": "Parashar",
"Date_Of_Birth": "1990-08-21",
"e_mail": "mahesh_parashar.123@gmail.com",
"phone": "9034343345"
}
{
"_id": {
"$oid": "696c230afed319cd950058c6"
},
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26",
"e_mail": "radhika_sharma.123@gmail.com",
"phone": "9000012345"
}
{
"_id": {
"$oid": "696c230afed319cd950058c7"
},
"First_Name": "Rachel",
"Last_Name": "Christopher",
"Date_Of_Birth": "1990-02-16",
"e_mail": "rachel_christopher.123@gmail.com",
"phone": "9000054321"
}
{
"_id": {
"$oid": "696c230afed319cd950058c8"
},
"First_Name": "Fathima",
"Last_Name": "Sheik",
"Date_Of_Birth": "1990-02-16",
"e_mail": "fathima_sheik.123@gmail.com",
"phone": "9000012345"
}