<?php
class CreateCollection {
protected $cmd = array();
function __construct($collectionName) {
$this->cmd["create"] = (string)$collectionName;
}
function setAutoIndexId($bool) {
$this->cmd["autoIndexId"] = (bool)$bool;
}
function setCappedCollection($maxBytes, $maxDocuments = false) {
$this->cmd["capped"] = true;
$this->cmd["size"] = (int)$maxBytes;
if ($maxDocuments) {
$this->cmd["max"] = (int)$maxDocuments;
}
}
function usePowerOf2Sizes($bool) {
if ($bool) {
$this->cmd["flags"] = 1;
} else {
$this->cmd["flags"] = 0;
}
}
function setFlags($flags) {
$this->cmd["flags"] = (int)$flags;
}
function getCommand() {
return new MongoDB\Driver\Command($this->cmd);
}
function getCollectionName() {
return $this->cmd["create"];
}
}
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$createCollection = new CreateCollection("cappedCollection");
$createCollection->setCappedCollection(64 * 1024);
try {
$command = $createCollection->getCommand();
$cursor = $manager->executeCommand("databaseName", $command);
$response = $cursor->toArray()[0];
var_dump($response);
$collstats = ["collstats" => $createCollection->getCollectionName()];
$cursor = $manager->executeCommand("databaseName", new MongoDB\Driver\Command($collstats));
$response = $cursor->toArray()[0];
var_dump($response);
} catch(MongoDB\Driver\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
?>