Skip to content

Commit de032a5

Browse files
committed
PHP-1303: Collection::distinct()
1 parent 865685e commit de032a5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

examples/write.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
foreach($result as $document) {
5959
var_dump($document);
6060
}
61+
$result = $collection->distinct("citizen");
62+
echo "Distinct countries:\n";
63+
var_dump($result);
6164

6265
$result = $collection->updateMany(
6366
array("citizen" => "Iceland"),

src/MongoDB/Collection.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ function getCountOptions() { /* {{{ */
264264
);
265265
} /* }}} */
266266

267+
function distinct($fieldName, array $filter = array(), array $options = array()) { /* {{{ */
268+
$options = array_merge($this->getFindOptions(), $options);
269+
$cmd = array(
270+
"distinct" => $this->collname,
271+
"key" => $fieldName,
272+
"query" => $filter,
273+
$options
274+
);
275+
276+
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
277+
if ($doc["ok"]) {
278+
return $doc["values"];
279+
}
280+
throw $this->_generateCommandException($doc);
281+
} /* }}} */
282+
function getDistinctOptions() { /* {{{ */
283+
return array(
284+
/**
285+
* The maximum amount of time to allow the query to run. The default is infinite.
286+
*
287+
* @see http://docs.mongodb.org/manual/reference/command/distinct/
288+
*/
289+
"maxTimeMS" => 0,
290+
);
291+
} /* }}} */
292+
267293
protected function _generateCommandException($doc) { /* {{{ */
268294
if ($doc["errmsg"]) {
269295
return new Exception($doc["errmsg"]);

0 commit comments

Comments
 (0)