Skip to content

PHP - Aggregation #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions code-examples/php/aggregation/agg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once "vendor/autoload.php";
$uri = getenv('MONGDODB_URI');
$client = new MongoDB\Client($uri);

// Begin data insertion
$db = $client->aggregation;
$coll = $db->restaurants;

// Create sample documents
$docs = [
['stars' => 3, 'categories' => ["Bakery", "Sandwiches"], 'name' => "Rising Sun Bakery"],
['stars' => 4, 'categories' => ["Bakery", "Cafe", "Bar"], 'name' => "Cafe au Late"],
['stars' => 5, 'categories' => ["Coffee", "Bakery"], 'name' => "Liz's Coffee Bar"],
['stars' => 3, 'categories' => ["Steak", "Seafood"], 'name' => "Oak Steakhouse"],
['stars' => 4, 'categories' => ["Bakery", "Dessert"], 'name' => "Petit Cookie"],
];

// Insert documents into the restaurants collection
$result = $coll->insertMany($docs);
// End data insertion

// Begin aggregation
// Define an aggregation pipeline with a match stage and a group stage
$pipeline = [
['$match' => ['categories' => "Bakery"]],
['$group' => ['_id' => '$stars', 'count' => ['$sum' => 1]]],
];

// Execute the aggregation
$aggCursor = $coll->aggregate($pipeline);

// Print the aggregated results
foreach ($aggCursor as $doc) {
var_dump($doc);
}
// End aggregation
?>
28 changes: 28 additions & 0 deletions code-examples/php/authentication/aws-env-variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

require 'vendor/autoload.php'; // Include necessary MongoDB PHP Driver libraries

use MongoDB\Client;

// Remember to specify your AWS credentials in environment variables.
$clusterUrl = "<MongoDB deployment url>";
$authMechanism = "MONGODB-AWS";

$uri =
"mongodb+srv://{$clusterUrl}/?authSource=%24external&authMechanism={$authMechanism}";

// Create a new Client instance.
$client = new Client($uri);

try {
// Establish and verify connection.
$client->admin->command(['ping' => 1]);
echo "Connected successfully to server.";
} catch (Exception $e) {
// Catch and output any connection errors.
echo $e->getMessage();
} finally {
// Ensure that the client is closed when it finishes/errors.
$client = null;
}
?>
34 changes: 34 additions & 0 deletions code-examples/php/authentication/aws.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require 'vendor/autoload.php'; // Include Composer's autoloader

// Replace the following with values for your environment.
$accessKeyId = urlencode("<AWS_ACCESS_KEY_ID>");
$secretAccessKey = urlencode("<AWS_SECRET_ACCESS_KEY>");
$clusterUrl = "<MongoDB cluster url>";

$authMechanism = "MONGODB-AWS";

$uri =
"mongodb+srv://{$accessKeyId}:{$secretAccessKey}@{$clusterUrl}/?authSource=%24external&authMechanism={$authMechanism}";

// Uncomment the following lines if your AWS authentication setup requires a session token.
// $sessionToken = urlencode("<AWS_SESSION_TOKEN>");
// $uri = $uri . "&authMechanismProperties=AWS_SESSION_TOKEN:{$sessionToken}";

// Create a new MongoClient.
$client = new MongoDB\Client($uri);

try {
// Establish and verify connection.
$command = new MongoDB\Driver\Command(['ping' => 1]);
$client->manager->executeCommand('admin', $command);
echo "Connected successfully to server.";
} catch (MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
echo $e->getTraceAsString(), "\n";
} finally {
// Ensure that the client closes when it finishes/errors.
$client = null;
}
?>
37 changes: 37 additions & 0 deletions code-examples/php/authentication/cr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

require_once './vendor/autoload.php';

// Replace the following with values for your environment.
$username = urlencode("<username>");
$password = urlencode("<password>");
$clusterUrl = "<MongoDB cluster url>";

// Replace the following with your MongoDB deployment's connection string.
$authMechanism = "SCRAM-SHA-1";
$clientPEMFile = "path_to_pem_file";

$uri =
"mongodb+srv://{$username}:{$password}@{$clusterUrl}/?authMechanism={$authMechanism}&ssl=true&sslPEMFile={$clientPEMFile}";

// Create a new MongoClient
$client = new MongoDB\Client($uri);

// Function to connect to the server
function run($client) {
try {
// Establish and verify connection
$response = $client->admin->command(['ping' => 1]);
echo "Connected successfully to server";

} catch (Exception $e) {
echo $e->getMessage(), "\n";
exit;
} finally {
// Ensures that the client will close when you finish/error
$client = null;
}
}

run($client);
?>
34 changes: 34 additions & 0 deletions code-examples/php/authentication/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require 'vendor/autoload.php';

$username = urlencode("<username>");
$password = urlencode("<password>");
$clusterUrl = "<MongoDB cluster url>";

$uri = "mongodb+srv://{$username}:{$password}@{$clusterUrl}?authSource=admin";

// Create a new MongoClient
$client = new MongoDB\Client($uri);

// Function to connect to the server
function run()
{
global $client;

try {
// Establish and verify connection
$command = new MongoDB\Driver\Command(['ping' => 1]);
$client->getManager()->executeCommand('admin', $command);

echo "Connected successfully to server.\n";
} catch (Exception $e) {
echo $e->getMessage(), "\n";
exit;
} finally {
// Ensures that the client will close when you finish/error
$client = null;
}
}

run();
39 changes: 39 additions & 0 deletions code-examples/php/authentication/sha1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

require 'vendor/autoload.php';

// Replace the following with values for your environment.
$username = "<username>";
$password = "<password>";
$clusterUrl = "<MongoDB cluster url>";

$authMechanism = "SCRAM-SHA-1";

// Replace the following with your MongoDB deployment's connection string.
$uri = sprintf('mongodb://%s:%s@%s/?authMechanism=%s',
$username,
$password,
$clusterUrl,
$authMechanism
);

// Create a new MongoDB driver manager instance.
$manager = new MongoDB\Driver\Manager($uri);

function run($manager) {
try {
// Establish and verify connection
$command = new MongoDB\Driver\Command(['ping' => 1]);
$manager->executeCommand("admin", $command);

echo "Connected successfully to server";
} catch (MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
echo $e->getTraceAsString(), "\n";
} finally {
// There's no need to "close" the connection in MongoDB PHP driver
}
}

run($manager);
?>
33 changes: 33 additions & 0 deletions code-examples/php/authentication/sha256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

require_once __DIR__ . "/vendor/autoload.php";

// Replace the following with your own values.
$username = "<username>";
$password = "<password>";
$clusterUrl = "<MongoDB cluster url>";

$authMechanism = "SCRAM-SHA-256";

// Replace the following with your MongoDB deployment's connection string.
$uri = sprintf('mongodb+srv://%s:%s@%s/?authMechanism=%s',
urlencode($username), urlencode($password), $clusterUrl, $authMechanism);

// Create a new MongoDB client
$client = new MongoDB\Client($uri);

try {
// Select the database and run the ping command
$command = new MongoDB\Driver\Command(['ping' => 1]);
$client->getManager()->executeCommand('admin', $command);

// If no exception thrown, we connected successfully.
echo "Connected successfully to server\n";

} catch(MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage(), "\n";
} finally {
// Close the connection
$client = null;
}
?>
31 changes: 31 additions & 0 deletions code-examples/php/authentication/x509.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
require_once __DIR__ . "/vendor/autoload.php";

// Replace the following with values for your environment.
$clusterUrl = "<MongoDB cluster url>";
$clientPEMFile = "<path to the client pem certificate file>";

$authMechanism = "MONGODB-X509";

// Replace the following with your MongoDB deployment's connection string.
$uri =
"mongodb+srv://$clusterUrl/?authMechanism=$authMechanism&ssl=true&sslPEMKeyFile=$clientPEMFile";

// Create a new MongoDB\Client
$client = new MongoDB\Client($uri);

// Function to connect to the server
function run($client) {
try {
// Establish and verify connection
$client->admin->command(['ping' => 1]);
echo "Connected successfully to server";
} catch (Exception $e) {
echo $e;
} finally {
// Ensures that the client will close when you finish/error
$client = null;
}
}
run($client);
?>