From fa5dfa607e40479f0ca835f89822c402561ba98d Mon Sep 17 00:00:00 2001 From: ERFANIUM Date: Sat, 14 Aug 2021 19:13:51 +0430 Subject: [PATCH] bug: fix countDocuments error when called without filter --- src/collection/collection.ts | 5 ++++- tests/cases/03_curd.ts | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/collection/collection.ts b/src/collection/collection.ts index 13943b1e..cd9917cc 100644 --- a/src/collection/collection.ts +++ b/src/collection/collection.ts @@ -94,7 +94,10 @@ export class Collection { filter?: Document, options?: CountOptions, ): Promise { - const pipeline: Document[] = [{ $match: filter }]; + const pipeline: Document[] = []; + if (filter) { + pipeline.push({ $match: filter }); + } if (typeof options?.skip === "number") { pipeline.push({ $skip: options.skip }); diff --git a/tests/cases/03_curd.ts b/tests/cases/03_curd.ts index a519674f..e7b3b1e0 100644 --- a/tests/cases/03_curd.ts +++ b/tests/cases/03_curd.ts @@ -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); });