Skip to content

Commit 865685e

Browse files
committed
PHP-1302: Collection::count()
1 parent d337c98 commit 865685e

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

examples/write.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@
3333
$result = $collection->insertOne($bobby);
3434
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
3535

36-
$result = $collection->find(array("nick" => "bjori"), array("projection" => array("name" => 1)));
37-
echo "Searching for nick => bjori, should have only one result:\n";
38-
foreach($result as $document) {
39-
var_dump($document);
40-
}
36+
$count = $collection->count(array("nick" => "bjori"));
37+
printf("Searching for nick => bjori, should have only one result: %d\n", $count);
4138

42-
$result = $collection->deleteOne($document);
43-
printf("Deleted: %s (out of expected 1)\n", $result->getNumRemoved());
4439
$result = $collection->updateOne(
4540
array("citizen" => "USA"),
4641
array('$set' => array("citizen" => "Iceland"))
@@ -52,33 +47,24 @@
5247
foreach($result as $document) {
5348
var_dump($document);
5449
}
55-
$result = $collection->deleteOne($document);
56-
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved());
57-
5850
} catch(Exception $e) {
5951
echo $e->getMessage(), "\n";
6052
exit;
6153
}
6254

6355
try {
64-
/* These two were removed earlier */
65-
$result = $collection->insertOne($hannes);
66-
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
67-
$result = $collection->insertOne($hayley);
68-
printf("Inserted: %s (out of expected 1)\n", $result->getNumInserted());
69-
7056
$result = $collection->find();
71-
echo "Find all docs, should be 3, verify 2x USA citizen, 1 Icelandic\n";
57+
echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
7258
foreach($result as $document) {
7359
var_dump($document);
7460
}
7561

7662
$result = $collection->updateMany(
77-
array("citizen" => "USA"),
78-
array('$set' => array("citizen" => "Iceland"))
63+
array("citizen" => "Iceland"),
64+
array('$set' => array("viking" => true))
7965
);
8066

81-
printf("Updated: %d (out of expected 2), verify everyone is Icelandic\n", $result->getNumModified());
67+
printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getNumModified());
8268
$result = $collection->find();
8369
foreach($result as $document) {
8470
var_dump($document);
@@ -93,8 +79,11 @@
9379
var_dump($document);
9480
}
9581

82+
$result = $collection->deleteOne($document);
83+
printf("Deleted: %d (out of expected 1)\n", $result->getNumRemoved());
84+
9685
$result = $collection->deleteMany(array("citizen" => "Iceland"));
97-
printf("Deleted: %d (out of expected 3)\n", $result->getNumRemoved());
86+
printf("Deleted: %d (out of expected 2)\n", $result->getNumRemoved());
9887
} catch(Exception $e) {
9988
echo $e->getMessage(), "\n";
10089
exit;

src/MongoDB/Collection.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* phongo includes */
55
use MongoDB\Manager;
66
use MongoDB\Query;
7+
use MongoDB\Command;
78
use MongoDB\ReadPreference;
89
use MongoDB\WriteBatch;
910

@@ -20,11 +21,14 @@ class Collection {
2021
protected $wc;
2122
protected $ns;
2223

24+
protected $dbname;
25+
protected $collname;
2326
function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null) {
2427
$this->manager = $manager;
2528
$this->ns = $ns;
2629
$this->wc = $wc;
2730
$this->rp = $rp;
31+
list($this->dbname, $this->collname) = explode(".", $ns, 2);
2832
}
2933

3034
function find(array $filter = array(), array $options = array()) { /* {{{ {{{ */
@@ -159,7 +163,7 @@ protected function _buildQuery($filter, $options) { /* {{{ */
159163
return $query;
160164
} /* }}} */
161165
/* }}} */
162-
166+
/* {{{ writes */
163167
protected function _writeSingle($filter, $type, array $options = array(), $newobj = array()) { /* {{{ */
164168
$options = array_merge($this->getWriteOptions(), $options);
165169

@@ -212,5 +216,64 @@ function replaceOne(array $filter, array $update, array $options = array()) { /*
212216
function updateMany(array $filter, $update, array $options = array()) { /* {{{ */
213217
return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update);
214218
} /* }}} */
219+
/* }}} */
220+
221+
function count(array $filter = array(), array $options = array()) { /* {{{ */
222+
$options = array_merge($this->getFindOptions(), $options);
223+
$cmd = array(
224+
"count" => $this->collname,
225+
"query" => $filter,
226+
$options
227+
);
228+
229+
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
230+
if ($doc["ok"]) {
231+
return $doc["n"];
232+
}
233+
throw $this->_generateCommandException($doc);
234+
} /* }}} */
235+
function getCountOptions() { /* {{{ */
236+
return array(
237+
/**
238+
* The index to use.
239+
*
240+
* @see http://docs.mongodb.org/manual/reference/command/count/
241+
*/
242+
"hint" => "", // string or document
243+
244+
/**
245+
* The maximum number of documents to count.
246+
*
247+
* @see http://docs.mongodb.org/manual/reference/command/count/
248+
*/
249+
"limit" => 0,
250+
251+
/**
252+
* The maximum amount of time to allow the query to run.
253+
*
254+
* @see http://docs.mongodb.org/manual/reference/command/count/
255+
*/
256+
"maxTimeMS" => 0,
257+
258+
/**
259+
* The number of documents to skip before returning the documents.
260+
*
261+
* @see http://docs.mongodb.org/manual/reference/command/count/
262+
*/
263+
"skip" => 0,
264+
);
265+
} /* }}} */
266+
267+
protected function _generateCommandException($doc) { /* {{{ */
268+
if ($doc["errmsg"]) {
269+
return new Exception($doc["errmsg"]);
270+
}
271+
var_dump($doc);
272+
return new Exception("FIXME: Unknown error");
273+
} /* }}} */
274+
protected function _runCommand($dbname, array $cmd, ReadPreference $rp = null) { /* {{{ */
275+
$command = new Command($cmd);
276+
return $this->manager->executeCommand($dbname, $command, $rp);
277+
} /* }}} */
215278
}
216279

0 commit comments

Comments
 (0)