Skip to content

Commit f6a37e9

Browse files
authored
roughly added estimated document count (#32)
Added support for the `estimatedDocumentCount` command; shouldn't need any further updates
1 parent 7e68ea1 commit f6a37e9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/data-api/collection.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,29 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
10151015
return resp.status?.count;
10161016
}
10171017

1018+
/**
1019+
* Gets an estimate of the count of documents in a collection.
1020+
*
1021+
* This operation is faster than {@link Collection.countDocuments} but may not be as accurate, and doesn't
1022+
* accept a filter. Unlike the former, **It can handle more than 1000 documents.**
1023+
*
1024+
* @remarks
1025+
* This gives a very rough estimate of the number of documents in the collection. It is not guaranteed to be
1026+
* accurate, and should not be used as a source of truth for the number of documents in the collection.
1027+
*
1028+
* @param options - The options for this operation.
1029+
*
1030+
* @returns The estimated number of documents in the collection
1031+
*/
1032+
public async estimatedDocumentCount(options?: WithTimeout): Promise<number> {
1033+
const command = {
1034+
estimatedDocumentCount: {},
1035+
};
1036+
1037+
const resp = await this._httpClient.executeCommand(command, options);
1038+
return resp.status?.count;
1039+
}
1040+
10181041
/**
10191042
* Atomically finds a single document in the collection and replaces it.
10201043
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// You may obtain a copy of the License at
2+
//
3+
// http://www.apache.org/licenses/LICENSE-2.0
4+
//
5+
// Unless required by applicable law or agreed to in writing, software
6+
// distributed under the License is distributed on an "AS IS" BASIS,
7+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8+
// See the License for the specific language governing permissions and
9+
// limitations under the License.
10+
// noinspection DuplicatedCode
11+
12+
import { Collection } from '@/src/data-api';
13+
import { initTestObjects } from '@/tests/fixtures';
14+
import assert from 'assert';
15+
16+
describe('integration.data-api.collection.estimated-document-count', () => {
17+
let collection: Collection;
18+
19+
before(async function () {
20+
[, , collection] = await initTestObjects(this);
21+
});
22+
23+
it('roughly works', async () => {
24+
const resp = await collection.estimatedDocumentCount();
25+
assert.ok(typeof <any>resp === 'number');
26+
assert.ok(resp >= 0);
27+
});
28+
});

0 commit comments

Comments
 (0)