PHP MongoDB context options

PHP can interact with MongoDB database through database extensions. For older versions of PHP, the mongo driver can be installed from PECL. This has now been replaced by the mongodb driver. Both drivers can be installed using precompiled binaries for Linux/Windows/MacOS operating systems. Alternatively, manual installation can be done from source tarball available on github. In either case, mongo or mongodb extension should be enabled in php.ini settings.

The PHP MongoDB extension provides Stream Context Support using the mongodb context. This allows you to register callback functions that are triggered during various MongoDB operations for debugging and monitoring purposes.

Syntax

$context = stream_context_create([
    'mongodb' => [
        'log_cmd_insert' => function($server, $document, $writeOptions, $protocolOptions) {
            // Log insert operations
        },
        'log_cmd_delete' => function($server, $writeOptions, $deleteOptions, $protocolOptions) {
            // Log delete operations  
        }
        // ... other context options
    ]
]);

Available Context Options

log_cmd_insert

log_cmd_insert(array $server, array $document, array $writeOptions, array $protocolOptions)

This is a callable function triggered when inserting a document into MongoDB.

log_cmd_delete

log_cmd_delete(array $server, array $writeOptions, array $deleteOptions, array $protocolOptions)

This callback function is triggered when deleting a document from MongoDB.

log_cmd_update

log_cmd_update(array $server, array $writeOptions, array $updateOptions, array $protocolOptions)

This function is used when updating a document in MongoDB.

log_write_batch

log_write_batch(array $server, array $writeOptions, array $batch, array $protocolOptions)

This function is triggered when executing a batch operation.

log_reply

log_reply(array $server, array $messageHeaders, array $operationHeaders)

This callback function is triggered when reading the MongoDB reply.

log_getmore

log_getmore(array $server, array $info)

This callable function is triggered when executing a GET_MORE operation to fetch additional results.

log_killcursor

log_killcursor(array $server, array $info)

Callback triggered when executing KILLCURSOR operations to close cursors.

Server Array Parameters

The $server array contains basic information with the following parameters:

  • hash − Server hash identifier (example: localhost:27017;-;X;56052)
  • type − Node type: primary/secondary/mongos/arbiter (example: 2)
  • max_bson_size − Maximum BSON size this node accepts (example: 16777216)
  • max_message_size − Maximum message size this node accepts (example: 48000000)
  • request_id − Request identifier for the message (example: 42)

Example Usage

<?php
$context = stream_context_create([
    'mongodb' => [
        'log_cmd_insert' => function($server, $document, $writeOptions, $protocolOptions) {
            error_log("Insert operation on server: " . $server['hash']);
        },
        'log_cmd_delete' => function($server, $writeOptions, $deleteOptions, $protocolOptions) {
            error_log("Delete operation on server: " . $server['hash']);
        }
    ]
]);

// Use the context with MongoDB operations
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017", [], ['context' => $context]);
?>

Conclusion

PHP MongoDB context options provide powerful debugging and monitoring capabilities by allowing you to register callback functions for various database operations. These callbacks help track database activity and troubleshoot performance issues in your MongoDB applications.

Updated on: 2026-03-15T09:21:32+05:30

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements