Content · Programming · PHP

PHP MongoDB\Driver\Manager::executeCommand() Method: Usage and Examples

Sends a MongoDB command document to a particular database. Command structure is command-specific; do not treat arbitrary client JSON as safe admin commands.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Sends a MongoDB command document to a particular database.

The examples focus on the operation itself, including the important distinction: executeQuery runs a read-only query object against a collection namespace.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


MongoDB\Driver\Manager::executeCommand(string $db, MongoDB\Driver\Command $command, ?array $options = null): MongoDB\Driver\Cursor

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$command = new MongoDB\Driver\Command(["ping" => 1]);
$cursor = $manager->executeCommand("admin", $command);
foreach ($cursor as $reply) { echo $reply->ok; }

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results. MongoDB commands have different response documents; inspect the fields returned by each specific command.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$command = new MongoDB\Driver\Command(["hello" => 1]);
$cursor = $manager->executeCommand("admin", $command);
foreach ($cursor as $reply) { echo $reply->ok; }

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results. MongoDB commands have different response documents; inspect the fields returned by each specific command.

<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$command = new MongoDB\Driver\Command(["buildInfo" => 1]);
$cursor = $manager->executeCommand("admin", $command);
foreach ($cursor as $reply) { echo json_encode($reply); }

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Command structure is command-specific; do not treat arbitrary client JSON as safe admin commands.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
MongoDB\Driver\Manager::executeCommandSends a MongoDB command document to a particular database.
Related API or alternativeexecuteQuery runs a read-only query object against a collection namespace.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsCommand structure is command-specific; do not treat arbitrary client JSON as safe admin commands.
Confusing it with a related APIexecuteQuery runs a read-only query object against a collection namespace.

Key Takeaways


  • Sends a MongoDB command document to a particular database.
  • Command structure is command-specific; do not treat arbitrary client JSON as safe admin commands.
  • executeQuery runs a read-only query object against a collection namespace.

Use MongoDB\Driver\Manager::executeCommand when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.