Commit de032a57 authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1303: Collection::distinct()

parent 865685e1
...@@ -58,6 +58,9 @@ try { ...@@ -58,6 +58,9 @@ try {
foreach($result as $document) { foreach($result as $document) {
var_dump($document); var_dump($document);
} }
$result = $collection->distinct("citizen");
echo "Distinct countries:\n";
var_dump($result);
$result = $collection->updateMany( $result = $collection->updateMany(
array("citizen" => "Iceland"), array("citizen" => "Iceland"),
......
...@@ -264,6 +264,32 @@ class Collection { ...@@ -264,6 +264,32 @@ class Collection {
); );
} /* }}} */ } /* }}} */
function distinct($fieldName, array $filter = array(), array $options = array()) { /* {{{ */
$options = array_merge($this->getFindOptions(), $options);
$cmd = array(
"distinct" => $this->collname,
"key" => $fieldName,
"query" => $filter,
$options
);
$doc = $this->_runCommand($this->dbname, $cmd)->getResponseDocument();
if ($doc["ok"]) {
return $doc["values"];
}
throw $this->_generateCommandException($doc);
} /* }}} */
function getDistinctOptions() { /* {{{ */
return array(
/**
* The maximum amount of time to allow the query to run. The default is infinite.
*
* @see http://docs.mongodb.org/manual/reference/command/distinct/
*/
"maxTimeMS" => 0,
);
} /* }}} */
protected function _generateCommandException($doc) { /* {{{ */ protected function _generateCommandException($doc) { /* {{{ */
if ($doc["errmsg"]) { if ($doc["errmsg"]) {
return new Exception($doc["errmsg"]); return new Exception($doc["errmsg"]);
......
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