Commit 865685e1 authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1302: Collection::count()

parent d337c985
......@@ -33,14 +33,9 @@ try {
$result = $collection->insertOne($bobby);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" => 1)));
echo "Searching for nick => bjori, should have only one result:\n";
foreach($result as $document) {
var_dump($document);
}
$count = $collection->count(array("nick" => "bjori"));
printf("Searching for nick => bjori, should have only one result: %d\n", $count);
$result = $collection->deleteOne($document);
printf("Deleted: %s (out of expected 1)\n", $result->getNumRemoved());
$result = $collection->updateOne(
array("citizen" => "USA"),
array('$set' => array("citizen" => "Iceland"))
......@@ -52,33 +47,24 @@ try {
foreach($result as $document) {
var_dump($document);
}
$result = $collection->deleteOne($document);
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved());
} catch(Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
try {
/* These two were removed earlier */
$result = $collection->insertOne($hannes);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->insertOne($hayley);
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
$result = $collection->find();
echo "Find all docs, should be 3, verify 2x USA citizen, 1 Icelandic\n";
echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
foreach($result as $document) {
var_dump($document);
}
$result = $collection->updateMany(
array("citizen" => "USA"),
array('$set' => array("citizen" => "Iceland"))
array("citizen" => "Iceland"),
array('$set' => array("viking" => true))
);
printf("Updated: %d (out of expected 2), verify everyone is Icelandic\n", $result->getNumModified());
printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getNumModified());
$result = $collection->find();
foreach($result as $document) {
var_dump($document);
......@@ -93,8 +79,11 @@ try {
var_dump($document);
}
$result = $collection->deleteOne($document);
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved());
$result = $collection->deleteMany(array("citizen" => "Iceland"));
printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved());
printf("Deleted: %d (out of expected 2)\n", $result->getNumRemoved());
} catch(Exception $e) {
echo $e->getMessage(), "\n";
exit;
......
......@@ -4,6 +4,7 @@ namespace MongoDB;
/* phongo includes */
use MongoDB\Manager;
use MongoDB\Query;
use MongoDB\Command;
use MongoDB\ReadPreference;
use MongoDB\WriteBatch;
......@@ -20,11 +21,14 @@ class Collection {
protected $wc;
protected $ns;
protected $dbname;
protected $collname;
function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null) {
$this->manager = $manager;
$this->ns = $ns;
$this->wc = $wc;
$this->rp = $rp;
list($this->dbname, $this->collname) = explode(".", $ns, 2);
}
function find(array $filter = array(), array $options = array()) { /* {{{ {{{ */
......@@ -159,7 +163,7 @@ class Collection {
return $query;
} /* }}} */
/* }}} */
/* {{{ writes */
protected function _writeSingle($filter, $type, array $options = array(), $newobj = array()) { /* {{{ */
$options = array_merge($this->getWriteOptions(), $options);
......@@ -212,5 +216,64 @@ class Collection {
function updateMany(array $filter, $update, array $options = array()) { /* {{{ */
return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update);
} /* }}} */
/* }}} */
function count(array $filter = array(), array $options = array()) { /* {{{ */
$options = array_merge($this->getFindOptions(), $options);
$cmd = array(
"count" => $this->collname,
"query" => $filter,
$options
);
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
if ($doc["ok"]) {
return $doc["n"];
}
throw $this->_generateCommandException($doc);
} /* }}} */
function getCountOptions() { /* {{{ */
return array(
/**
* The index to use.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"hint" => "", // string or document
/**
* The maximum number of documents to count.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"limit" => 0,
/**
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"maxTimeMS" => 0,
/**
* The number of documents to skip before returning the documents.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"skip" => 0,
);
} /* }}} */
protected function _generateCommandException($doc) { /* {{{ */
if ($doc["errmsg"]) {
return new Exception($doc["errmsg"]);
}
var_dump($doc);
return new Exception("FIXME: Unknown error");
} /* }}} */
protected function _runCommand($dbname, array $cmd, ReadPreference $rp = null) { /* {{{ */
$command = new Command($cmd);
return $this->manager->executeCommand($dbname, $command, $rp);
} /* }}} */
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment