Skip to content

Commit

Permalink
bug: fix countDocuments error when called without filter
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanium committed Aug 14, 2021
1 parent 3f32126 commit fa5dfa6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export class Collection<T> {
filter?: Document,
options?: CountOptions,
): Promise<number> {
const pipeline: Document[] = [{ $match: filter }];
const pipeline: Document[] = [];
if (filter) {
pipeline.push({ $match: filter });
}

if (typeof options?.skip === "number") {
pipeline.push({ $skip: options.skip });
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/03_curd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ export default function curdTests() {
testWithClient("testCountDocuments", async (client) => {
const db = client.database("test");
const users = db.collection("mongo_test_users");
const countAll = await users.countDocuments();
assertEquals(countAll, 4);

const count = await users.countDocuments({ username: "many" });
assertEquals(count, 2);
});
Expand Down

0 comments on commit fa5dfa6

Please sign in to comment.