Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add countDocuments and estimatedDocumentCount methods and deprecate count method #222

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export class Collection<T> {
return result.value;
}

/**
* @deprecated Use `countDocuments` or `estimatedDocumentCount` instead
*/
async count(filter?: Document, options?: CountOptions): Promise<number> {
const res = await this.#protocol.commandSingle(this.#dbName, {
count: this.name,
Expand All @@ -87,6 +90,43 @@ export class Collection<T> {
}
}

async countDocuments(
filter?: Document,
options?: CountOptions,
): Promise<number> {
const pipeline: Document[] = [{ $match: filter }];

if (typeof options?.skip === "number") {
pipeline.push({ $skip: options.skip });
delete options.skip;
}

if (typeof options?.limit === "number") {
pipeline.push({ $limit: options.limit });
delete options.limit;
}

pipeline.push({ $group: { _id: 1, n: { $sum: 1 } } });

const result = await this.aggregate<{ n: number }>(
pipeline,
options,
).next();
if (result) return result.n;
return 0;
}

async estimatedDocumentCount(): Promise<number> {
const pipeline = [
{ $collStats: { count: {} } },
{ $group: { _id: 1, n: { $sum: "$count" } } },
];

const result = await this.aggregate<{ n: number }>(pipeline).next();
if (result) return result.n;
return 0;
}

async insertOne(doc: Document, options?: InsertOptions) {
const { insertedIds } = await this.insertMany([doc], options);
return insertedIds[0];
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/03_curd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ export default function curdTests() {
assertEquals(count, 2);
});

testWithClient("testCountDocuments", async (client) => {
const db = client.database("test");
const users = db.collection("mongo_test_users");
const count = await users.countDocuments({ username: "many" });
assertEquals(count, 2);
});

testWithClient("testEstimatedDocumentCount", async (client) => {
const db = client.database("test");
const users = db.collection("mongo_test_users");
const count = await users.estimatedDocumentCount();
assertEquals(count, 4);
});

testWithClient("testAggregation", async (client) => {
const db = client.database("test");
const users = db.collection("mongo_test_users");
Expand Down