Mongolian equivalent of SQL SELECT DISTINCT?

According to the heading, what will PHP Mongo be the equivalent of something like this in SQL:

SELECT DISTINCT(field) FROM table WHERE someCondition = 1 

I read the article this table , but I do not see how to match db.users.distinct('last_name') in PHP.

+3
php mongodb distinct
Mar 08 '11 at 17:37
source share
2 answers

Just issue command and set distinct key.

Take a look at the following example from the docs:

Search for all the different values ​​for a key.

 <?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"; } ?> 

In the above example, something similar to:

 4 22 87 
+4
Mar 08 '11 at 17:40
source share

If you need to add a where clause, use the following syntax:

 $ages = $db->command(array( "distinct" => "people", "key" => "age", "query" => array("someField" => "someValue"))); 
+4
Jul 10 2018-12-12T00:
source share



All Articles