Skip to content

Commit 5fbc340

Browse files
committed
Add distinct()
1 parent 1a5f602 commit 5fbc340

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MongoDB for XP Framework ChangeLog
33

44
## ?.?.? / ????-??-??
55

6-
* Added new method `Collection::count()` to count documents in a collection
6+
* Added new methods `Collection::distinct()` and `Collection::count()`
77
(@thekid)
88
* Added new method `Collection::aggregate()` to perform aggregations, see
99
https://docs.mongodb.com/manual/reference/command/aggregate/ and

src/main/php/com/mongodb/Collection.class.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function find($filter= []): Cursor {
8989
* Count documents in this collection
9090
*
9191
* @param [:var] $filter
92-
* @return com.mongodb.result.Cursor
92+
* @return int
9393
*/
9494
public function count($filter= []): int {
9595
$result= $this->proto->msg(0, 0, [
@@ -100,6 +100,23 @@ public function count($filter= []): int {
100100
return $result['body']['n'];
101101
}
102102

103+
/**
104+
* Returns distinct list of keys in this collection
105+
*
106+
* @param string $key
107+
* @param [:var] $filter
108+
* @return var[]
109+
*/
110+
public function distinct($key, $filter= []): array {
111+
$result= $this->proto->msg(0, 0, [
112+
'distinct' => $this->name,
113+
'key' => $key,
114+
'query' => $filter ?: (object)[],
115+
'$db' => $this->database,
116+
]);
117+
return $result['body']['values'];
118+
}
119+
103120
/**
104121
* Perfom aggregation over documents this collection
105122
*

0 commit comments

Comments
 (0)