Skip to content

Commit 1c30539

Browse files
authored
Use estimatedDocumentCount to Get Rough Document Count (#226)
2 parents 35221cb + 18a6fd4 commit 1c30539

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/documentdb/ClustersClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ export class ClustersClient {
368368
return count;
369369
}
370370

371+
async estimateDocumentCount(databaseName: string, collectionName: string): Promise<number> {
372+
const collection = this._mongoClient.db(databaseName).collection(collectionName);
373+
374+
return await collection.estimatedDocumentCount();
375+
}
376+
371377
async *streamDocuments(
372378
databaseName: string,
373379
collectionName: string,

src/services/tasks/copy-and-paste/documentdb/documentDbDocumentReader.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ export class DocumentDbDocumentReader implements DocumentReader {
4040
*
4141
* @param connectionId Connection identifier to get the DocumentDB client
4242
* @param databaseName Name of the database
43-
* @param collectionName Name of the collection,
44-
* @param filter Optional filter to apply to the count operation (default is '{}')
43+
* @param collectionName Name of the collection
4544
* @returns Promise resolving to the document count
4645
*/
47-
async countDocuments(
48-
connectionId: string,
49-
databaseName: string,
50-
collectionName: string,
51-
filter: string = '{}',
52-
): Promise<number> {
46+
async countDocuments(connectionId: string, databaseName: string, collectionName: string): Promise<number> {
5347
const client = await ClustersClient.getClient(connectionId);
54-
return await client.countDocuments(databaseName, collectionName, filter);
48+
// Currently we use estimatedDocumentCount to get a rough idea of the document count
49+
// estimatedDocumentCount evaluates document counts based on metadata with O(1) complexity
50+
// We gain performance benefits by avoiding a full collection scan, especially for large collections
51+
//
52+
// NOTE: estimatedDocumentCount doesn't support filtering
53+
// so we need to provide alternative count method for filtering implementation in later iteration
54+
return await client.estimateDocumentCount(databaseName, collectionName);
5555
}
5656
}

0 commit comments

Comments
 (0)