diff --git a/types/mongodb/index.d.ts b/types/mongodb/index.d.ts index dd4f44f38a..28173780a8 100644 --- a/types/mongodb/index.d.ts +++ b/types/mongodb/index.d.ts @@ -482,13 +482,13 @@ export interface Collection { createIndexes(indexSpecs: Object[], options?: {session?: ClientSession}): Promise; createIndexes(indexSpecs: Object[], options: {session?: ClientSession}, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#deleteMany */ - deleteMany(filter: Object, callback: MongoCallback): void; - deleteMany(filter: Object, options?: CommonOptions): Promise; - deleteMany(filter: Object, options: CommonOptions, callback: MongoCallback): void; + deleteMany(filter: FilterQuery, callback: MongoCallback): void; + deleteMany(filter: FilterQuery, options?: CommonOptions): Promise; + deleteMany(filter: FilterQuery, options: CommonOptions, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#deleteOne */ - deleteOne(filter: Object, callback: MongoCallback): void; - deleteOne(filter: Object, options?: CommonOptions & { bypassDocumentValidation?: boolean }): Promise; - deleteOne(filter: Object, options: CommonOptions & { bypassDocumentValidation?: boolean }, callback: MongoCallback): void; + deleteOne(filter: FilterQuery, callback: MongoCallback): void; + deleteOne(filter: FilterQuery, options?: CommonOptions & { bypassDocumentValidation?: boolean }): Promise; + deleteOne(filter: FilterQuery, options: CommonOptions & { bypassDocumentValidation?: boolean }, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#distinct */ distinct(key: string, query: Object, callback: MongoCallback): void; distinct(key: string, query: Object, options?: { readPreference?: ReadPreference | string, maxTimeMS?: number, session?: ClientSession }): Promise; @@ -506,25 +506,25 @@ export interface Collection { dropIndexes(callback?: MongoCallback): void; dropIndexes(options: {session?: ClientSession, maxTimeMS?: number}, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find */ - find(query?: Object): Cursor; + find(query?: FilterQuery): Cursor; /** @deprecated */ - find(query: Object, options?: FindOneOptions): Cursor; + find(query: FilterQuery, options?: FindOneOptions): Cursor; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOne */ - findOne(filter: Object, callback: MongoCallback): void; - findOne(filter: Object, options?: FindOneOptions): Promise; - findOne(filter: Object, options: FindOneOptions, callback: MongoCallback): void; + findOne(filter: FilterQuery, callback: MongoCallback): void; + findOne(filter: FilterQuery, options?: FindOneOptions): Promise; + findOne(filter: FilterQuery, options: FindOneOptions, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOneAndDelete */ - findOneAndDelete(filter: Object, callback: MongoCallback>): void; - findOneAndDelete(filter: Object, options?: { projection?: Object, sort?: Object, maxTimeMS?: number, session?: ClientSession }): Promise>; - findOneAndDelete(filter: Object, options: { projection?: Object, sort?: Object, maxTimeMS?: number, session?: ClientSession }, callback: MongoCallback>): void; + findOneAndDelete(filter: FilterQuery, callback: MongoCallback>): void; + findOneAndDelete(filter: FilterQuery, options?: { projection?: Object, sort?: Object, maxTimeMS?: number, session?: ClientSession }): Promise>; + findOneAndDelete(filter: FilterQuery, options: { projection?: Object, sort?: Object, maxTimeMS?: number, session?: ClientSession }, callback: MongoCallback>): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOneAndReplace */ - findOneAndReplace(filter: Object, replacement: Object, callback: MongoCallback>): void; - findOneAndReplace(filter: Object, replacement: Object, options?: FindOneAndReplaceOption): Promise>; - findOneAndReplace(filter: Object, replacement: Object, options: FindOneAndReplaceOption, callback: MongoCallback>): void; + findOneAndReplace(filter: FilterQuery, replacement: Object, callback: MongoCallback>): void; + findOneAndReplace(filter: FilterQuery, replacement: Object, options?: FindOneAndReplaceOption): Promise>; + findOneAndReplace(filter: FilterQuery, replacement: Object, options: FindOneAndReplaceOption, callback: MongoCallback>): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOneAndUpdate */ - findOneAndUpdate(filter: Object, update: Object, callback: MongoCallback>): void; - findOneAndUpdate(filter: Object, update: Object, options?: FindOneAndReplaceOption): Promise>; - findOneAndUpdate(filter: Object, update: Object, options: FindOneAndReplaceOption, callback: MongoCallback>): void; + findOneAndUpdate(filter: FilterQuery, update: Object, callback: MongoCallback>): void; + findOneAndUpdate(filter: FilterQuery, update: Object, options?: FindOneAndReplaceOption): Promise>; + findOneAndUpdate(filter: FilterQuery, update: Object, options: FindOneAndReplaceOption, callback: MongoCallback>): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#geoHaystackSearch */ geoHaystackSearch(x: number, y: number, callback: MongoCallback): void; geoHaystackSearch(x: number, y: number, options?: GeoHaystackSearchOptions): Promise; @@ -602,9 +602,9 @@ export interface Collection { rename(newName: string, options?: { dropTarget?: boolean, session?: ClientSession }): Promise>; rename(newName: string, options: { dropTarget?: boolean, session?: ClientSession }, callback: MongoCallback>): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#replaceOne */ - replaceOne(filter: Object, doc: Object, callback: MongoCallback): void; - replaceOne(filter: Object, doc: Object, options?: ReplaceOneOptions): Promise; - replaceOne(filter: Object, doc: Object, options: ReplaceOneOptions, callback: MongoCallback): void; + replaceOne(filter: FilterQuery, doc: Object, callback: MongoCallback): void; + replaceOne(filter: FilterQuery, doc: Object, options?: ReplaceOneOptions): Promise; + replaceOne(filter: FilterQuery, doc: Object, options: ReplaceOneOptions, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#save */ /** @deprecated Use insertOne, insertMany, updateOne or updateMany */ save(doc: Object, callback: MongoCallback): void; @@ -618,23 +618,62 @@ export interface Collection { stats(options: { scale: number, session?: ClientSession }, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#update */ /** @deprecated use updateOne, updateMany or bulkWrite */ - update(filter: Object, update: Object, callback: MongoCallback): void; + update(filter: FilterQuery, update: Object, callback: MongoCallback): void; /** @deprecated use updateOne, updateMany or bulkWrite */ - update(filter: Object, update: Object, options?: ReplaceOneOptions & { multi?: boolean }): Promise; + update(filter: FilterQuery, update: Object, options?: ReplaceOneOptions & { multi?: boolean }): Promise; /** @deprecated use updateOne, updateMany or bulkWrite */ - update(filter: Object, update: Object, options: ReplaceOneOptions & { multi?: boolean }, callback: MongoCallback): void; + update(filter: FilterQuery, update: Object, options: ReplaceOneOptions & { multi?: boolean }, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#updateMany */ - updateMany(filter: Object, update: Object, callback: MongoCallback): void; - updateMany(filter: Object, update: Object, options?: CommonOptions & { upsert?: boolean }): Promise; - updateMany(filter: Object, update: Object, options: CommonOptions & { upsert?: boolean }, callback: MongoCallback): void; + updateMany(filter: FilterQuery, update: Object, callback: MongoCallback): void; + updateMany(filter: FilterQuery, update: Object, options?: CommonOptions & { upsert?: boolean }): Promise; + updateMany(filter: FilterQuery, update: Object, options: CommonOptions & { upsert?: boolean }, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#updateOne */ - updateOne(filter: Object, update: Object, callback: MongoCallback): void; - updateOne(filter: Object, update: Object, options?: ReplaceOneOptions): Promise; - updateOne(filter: Object, update: Object, options: ReplaceOneOptions, callback: MongoCallback): void; + updateOne(filter: FilterQuery, update: Object, callback: MongoCallback): void; + updateOne(filter: FilterQuery, update: Object, options?: ReplaceOneOptions): Promise; + updateOne(filter: FilterQuery, update: Object, options: ReplaceOneOptions, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#watch */ watch(pipeline?: Object[], options?: ChangeStreamOptions & { session?: ClientSession }): ChangeStream; } +type FilterQuery = { + [P in keyof T]?: T[P] | { + $eq?: T[P]; + $gt?: T[P]; + $gte?: T[P]; + $in?: T[P][]; + $lt?: T[P]; + $lte?: T[P]; + $ne?: T[P]; + $nin?: T[P][]; + $and?: (FilterQuery | T[P])[]; + $or?: (FilterQuery | T[P])[]; + $not?: (FilterQuery | T[P])[] | T[P]; + $expr?: any; + $jsonSchema?: any; + $mod?: [number, number]; + $regex?: RegExp; + $options?: string; + $text?: { + $search: string; + $language?: string; + $caseSensitive?: boolean; + $diacraticSensitive?: boolean; + }; + $where: Object; + $geoIntersects?: Object; + $geoWithin?: Object; + $near?: Object; + $nearSphere?: Object; + $elemMatch?: Object; + $size?: number; + $bitsAllClear?: Object; + $bitsAllSet?: Object; + $bitsAnyClear?: Object; + $bitsAnySet?: Object; + [key: string]: any; + }; +} | { [key:string]: any }; + // Documentation: http://docs.mongodb.org/manual/reference/command/collStats/ export interface CollStats { // Namespace. diff --git a/types/mongodb/mongodb-tests.ts b/types/mongodb/mongodb-tests.ts index 36428d786f..852522aadc 100644 --- a/types/mongodb/mongodb-tests.ts +++ b/types/mongodb/mongodb-tests.ts @@ -98,4 +98,19 @@ MongoClient.connect('mongodb://127.0.0.1:27017/test', options, function (err: mo type payment = typeof payment; let cursor: mongodb.AggregationCursor = collection.aggregate([{}]) } + + // test for new typings + { + type TestCollection = { + stringField: string; + numberField: number; + }; + let testCollection = db.collection('testCollection'); + + testCollection.find({ + numberField: { + $and: [{ $gt: 0, $lt: 100 }] + } + }); + } })