Merge pull request #26861 from mjomble/mongodb-count

mongodb: improved overloads for Collection.count()
This commit is contained in:
Paul van Brenk
2018-06-26 15:40:49 -07:00
committed by GitHub
2 changed files with 25 additions and 2 deletions
+2 -1
View File
@@ -487,8 +487,9 @@ export interface Collection<TSchema = Default> {
bulkWrite(operations: Object[], options?: CollectionBluckWriteOptions): Promise<BulkWriteOpResultObject>;
bulkWrite(operations: Object[], options: CollectionBluckWriteOptions, callback: MongoCallback<BulkWriteOpResultObject>): void;
/** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#count */
count(callback: MongoCallback<number>): void;
count(query: Object, callback: MongoCallback<number>): void;
count(query: Object, options?: MongoCountPreferences): Promise<number>;
count(query?: Object, options?: MongoCountPreferences): Promise<number>;
count(query: Object, options: MongoCountPreferences, callback: MongoCallback<number>): void;
/** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#createIndex */
createIndex(fieldOrSpec: string | any, callback: MongoCallback<string>): void;
+23 -1
View File
@@ -35,7 +35,29 @@ MongoClient.connect('mongodb://127.0.0.1:27017/test', options, function (err: mo
var collection = db.collection('test_insert');
collection.insertOne({ a: 2 }, function (err: mongodb.MongoError, docs: any) {
collection.count(function (err: mongodb.MongoError, count: any) {
// Intentionally omitted type annotation from 'count'.
// This way it requires a more accurate typedef which allows inferring that it's a number.
collection.count(function (err: mongodb.MongoError, count) {
console.log(format("count = %s", count));
});
collection.count().then(function (count: number) {
console.log(format("count = %s", count));
});
collection.count({ foo: 1 }, function (err: mongodb.MongoError, count: number) {
console.log(format("count = %s", count));
});
collection.count({ foo: 1 }).then(function (count: number) {
console.log(format("count = %s", count));
});
collection.count({ foo: 1 }, { limit: 10 }, function (err: mongodb.MongoError, count: number) {
console.log(format("count = %s", count));
});
collection.count({ foo: 1 }, { limit: 10 }).then(function (count: number) {
console.log(format("count = %s", count));
});