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); });