Skip to content

Commit 1a5f602

Browse files
committed
Add count() method
1 parent fe3405d commit 1a5f602

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ MongoDB for XP Framework ChangeLog
33

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

6+
* Added new method `Collection::count()` to count documents in a collection
7+
(@thekid)
68
* Added new method `Collection::aggregate()` to perform aggregations, see
79
https://docs.mongodb.com/manual/reference/command/aggregate/ and
810
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ public function find($filter= []): Cursor {
8585
return new Cursor($this->proto, $result['body']['cursor']);
8686
}
8787

88+
/**
89+
* Count documents in this collection
90+
*
91+
* @param [:var] $filter
92+
* @return com.mongodb.result.Cursor
93+
*/
94+
public function count($filter= []): int {
95+
$result= $this->proto->msg(0, 0, [
96+
'count' => $this->name,
97+
'query' => $filter ?: (object)[],
98+
'$db' => $this->database,
99+
]);
100+
return $result['body']['n'];
101+
}
102+
88103
/**
89104
* Perfom aggregation over documents this collection
90105
*

0 commit comments

Comments
 (0)