File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
tests/integration/data-api/collection Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -1015,6 +1015,29 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
1015
1015
return resp . status ?. count ;
1016
1016
}
1017
1017
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
+
1018
1041
/**
1019
1042
* Atomically finds a single document in the collection and replaces it.
1020
1043
*
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments