mongodb怎么实现DISTINCT查询

Mysql实现排重查询可以这样:

<br />
SELECT DISTINCT(field) FROM table WHERE someCondition = 1;<br />

如果在mongodb平台上怎么实现呢?

可以这样实现:

<br />
<?php

$people = $db->people;</p>
<p>$people->insert(array("name" => "Joe", "age" => 4));<br />
$people->insert(array("name" => "Sally", "age" => 22));<br />
$people->insert(array("name" => "Dave", "age" => 22));<br />
$people->insert(array("name" => "Molly", "age" => 87));</p>
<p>$ages=$db->command(array("distinct"=>"people","key"=>"age"));</p>
<p>foreach ($ages['values'] as $age) {<br />
    echo "$age\n";<br />
}</p>
<p>?><br />

The above example will output something similar to:
<br />
4<br />
22<br />
87<br />

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.