Sokil\Mongo\Collection::getDistinct PHP Method

getDistinct() public method

Retrieve a list of distinct values for the given key across a collection.
public getDistinct ( string $selector, array | callable | Expression $expression = null ) : array
$selector string field selector
$expression array | callable | Expression expression to search documents
return array distinct values
    public function getDistinct($selector, $expression = null)
    {
        if ($expression) {
            return $this->getMongoCollection()->distinct($selector, Expression::convertToArray($expression));
        }
        return $this->getMongoCollection()->distinct($selector);
    }

Usage Example

Example #1
0
 public function testGetDistinct_NoExpression()
 {
     $this->collection->createDocument(array('k' => array('f' => 'F1', 'kk' => 'A')))->save();
     $this->collection->createDocument(array('k' => array('f' => 'F1', 'kk' => 'A')))->save();
     $this->collection->createDocument(array('k' => array('f' => 'F1', 'kk' => 'B')))->save();
     $this->collection->createDocument(array('k' => array('f' => 'F2', 'kk' => 'C')))->save();
     // get distinct
     $distinctValues = $this->collection->getDistinct('k.kk');
     $this->assertEquals(array('A', 'B', 'C'), $distinctValues);
 }