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 - Sorting Records
Steps to Sort Documents 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 sort documents and select document using find() command while passing an option to sort records in descending order by name.
// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');
// Define a Filter to select all records
$filter = [];
// Define a option to sort Records
$options = ['sort' => ['First_Name' => -1]];
// execute the query
$cursor = $collection->find($filter, $options);
// Iterate over the cursor to access each document
foreach ($cursor as $document) {
echo "<pre>";
echo json_encode($document, JSON_PRETTY_PRINT);
echo "</pre>";
}
Example - Sorting Documents of a MongoDB Collection in Descending Order
Try the following example to sort documents of a MongoDB collection in descending order−
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 to select all records
$filter = [];
// Define a option to sort Records
$options = ['sort' => ['First_Name' => -1]];
// execute the query
$cursor = $collection->find($filter, $options);
// 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": "696cf16bfed319cd950058d5"
},
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26",
"e_mail": "radhika_sharma.123@gmail.com",
"phone": "9000012345"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d6"
},
"First_Name": "Rachel",
"Last_Name": "Christopher",
"Date_Of_Birth": "1990-02-16",
"e_mail": "rachel_christopher.123@gmail.com",
"phone": "9000054321"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d4"
},
"First_Name": "Mahesh",
"Last_Name": "Parashar",
"Date_Of_Birth": "1990-08-21",
"e_mail": "mahesh_parashar.123@gmail.com",
"phone": "9034343345"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d7"
},
"First_Name": "Fathima",
"Last_Name": "Sheik",
"Date_Of_Birth": "1990-02-16",
"e_mail": "fathima_sheik.123@gmail.com",
"phone": "9000012345"
}
Example - Sorting Documents of a MongoDB Collection in Ascending Order
Try the following example to sort documents of a MongoDB collection in ascending order −
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 to select all records
$filter = [];
// Define a option to sort Records
$options = ['sort' => ['First_Name' => 1]];
// execute the query
$cursor = $collection->find($filter, $options);
// 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": "696cf16bfed319cd950058d7"
},
"First_Name": "Fathima",
"Last_Name": "Sheik",
"Date_Of_Birth": "1990-02-16",
"e_mail": "fathima_sheik.123@gmail.com",
"phone": "9000012345"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d4"
},
"First_Name": "Mahesh",
"Last_Name": "Parashar",
"Date_Of_Birth": "1990-08-21",
"e_mail": "mahesh_parashar.123@gmail.com",
"phone": "9034343345"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d6"
},
"First_Name": "Rachel",
"Last_Name": "Christopher",
"Date_Of_Birth": "1990-02-16",
"e_mail": "rachel_christopher.123@gmail.com",
"phone": "9000054321"
}
{
"_id": {
"$oid": "696cf16bfed319cd950058d5"
},
"First_Name": "Radhika",
"Last_Name": "Sharma",
"Date_Of_Birth": "1995-09-26",
"e_mail": "radhika_sharma.123@gmail.com",
"phone": "9000012345"
}
Advertisements