mongodb怎么实现DISTINCT查询

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

SELECT DISTINCT(field) FROM table WHERE someCondition = 1;

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

可以这样实现:

<?php

$people = $db->people;

$people->insert(array("name" => "Joe", "age" => 4));
$people->insert(array("name" => "Sally", "age" => 22));
$people->insert(array("name" => "Dave", "age" => 22));
$people->insert(array("name" => "Molly", "age" => 87));

$ages=$db->command(array("distinct"=>"people","key"=>"age"));

foreach ($ages['values'] as $age) {
    echo "$age\n";
}

?>

The above example will output something similar to:

4 
22 
87

相关文章

发表评论

返回顶部