Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created June 16, 2017 15:45
Show Gist options
  • Save jmikola/384fe6153b3edf157a31f53828018cbc to your computer and use it in GitHub Desktop.
Save jmikola/384fe6153b3edf157a31f53828018cbc to your computer and use it in GitHub Desktop.
Profiling MongoDB queries
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client();
$db = $client->selectDatabase('test');
// Disable profiling before dropping system.profile
$db->command(['profile' => 0]);
$profileCollection = $db->selectCollection('system.profile');
$profileCollection->drop();
// Enable profiling for all queries
$db->command(['profile' => 2]);
$collection = $db->selectCollection('foo');
$collection->drop();
$collection->insertOne(['x' => 1]);
$collection->findOne(['x' => 1]);
// Disable profiling again
$db->command(['profile' => 0]);
// Query profile for all insert operations
foreach ($profileCollection->find(['op' => 'insert']) as $document) {
var_dump($document);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment