Skip to content

Commit

Permalink
Merge pull request #229 from erfanium/e4
Browse files Browse the repository at this point in the history
bug: fix `countDocuments` error when called without filter
  • Loading branch information
hackers267 authored Aug 14, 2021
2 parents 3f32126 + fa5dfa6 commit 1dc29e1
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 1dc29e1

Please sign in to comment.