declare module 'mongoose' { import mongodb = require('mongodb'); export interface AcceptsDiscriminator { /** Adds a discriminator type. */ discriminator(name: string | number, schema: Schema, value?: string | number | ObjectId): Model; discriminator(name: string | number, schema: Schema, value?: string | number | ObjectId): U; } interface MongooseBulkWriteOptions { skipValidation?: boolean; } interface InsertManyOptions extends PopulateOption, SessionOption { limit?: number; rawResult?: boolean; ordered?: boolean; lean?: boolean; } type InsertManyResult = mongodb.InsertManyResult & { insertedIds: { [key: number]: InferId; }; mongoose?: { validationErrors?: Array }; }; type UpdateWriteOpResult = mongodb.UpdateResult; interface MapReduceOptions { map: Function | string; reduce: (key: K, vals: T[]) => R; /** query filter object. */ query?: any; /** sort input objects using this key */ sort?: any; /** max number of documents */ limit?: number; /** keep temporary data default: false */ keeptemp?: boolean; /** finalize function */ finalize?: (key: K, val: R) => R; /** scope variables exposed to map/reduce/finalize during execution */ scope?: any; /** it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X default: false */ jsMode?: boolean; /** provide statistics on job execution time. default: false */ verbose?: boolean; readPreference?: string; /** sets the output target for the map reduce job. default: {inline: 1} */ out?: { /** the results are returned in an array */ inline?: number; /** * {replace: 'collectionName'} add the results to collectionName: the * results replace the collection */ replace?: string; /** * {reduce: 'collectionName'} add the results to collectionName: if * dups are detected, uses the reducer / finalize functions */ reduce?: string; /** * {merge: 'collectionName'} add the results to collectionName: if * dups exist the new docs overwrite the old */ merge?: string; }; } interface GeoSearchOptions { /** x,y point to search for */ near: number[]; /** the maximum distance from the point near that a result can be */ maxDistance: number; /** The maximum number of results to return */ limit?: number; /** return the raw object instead of the Mongoose Model */ lean?: boolean; } interface ModifyResult { value: Require_id | null; /** see https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject */ lastErrorObject?: { updatedExisting?: boolean; upserted?: mongodb.ObjectId; }; ok: 0 | 1; } type WriteConcern = mongodb.WriteConcern; /** A list of paths to validate. If set, Mongoose will validate only the modified paths that are in the given list. */ type PathsToValidate = string[] | string; /** * @deprecated */ type pathsToValidate = PathsToValidate; interface SaveOptions extends SessionOption { checkKeys?: boolean; j?: boolean; safe?: boolean | WriteConcern; timestamps?: boolean | QueryTimestampsConfig; validateBeforeSave?: boolean; validateModifiedOnly?: boolean; w?: number | string; wtimeout?: number; } interface RemoveOptions extends SessionOption, Omit {} const Model: Model; interface Model extends NodeJS.EventEmitter, AcceptsDiscriminator, IndexManager, SessionStarter { new (doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument, TVirtuals>> & ObtainSchemaGeneric; aggregate(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback): Aggregate>; aggregate(pipeline: PipelineStage[], callback?: Callback): Aggregate>; /** Base Mongoose instance the model uses. */ base: Mongoose; /** * If this is a discriminator model, `baseModelName` is the name of * the base model. */ baseModelName: string | undefined; /* Cast the given POJO to the model's schema */ castObject(obj: AnyObject, options?: { ignoreCastErrors?: boolean }): T; /** * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`, * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one * command. This is faster than sending multiple independent operations (e.g. * if you use `create()`) because with `bulkWrite()` there is only one network * round trip to the MongoDB server. */ bulkWrite(writes: Array>, options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions, callback: Callback): void; bulkWrite(writes: Array>, callback: Callback): void; bulkWrite(writes: Array>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions): Promise; /** * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than * sending multiple `save()` calls because with `bulkSave()` there is only one * network round trip to the MongoDB server. */ bulkSave(documents: Array, options?: mongodb.BulkWriteOptions & { timestamps?: boolean }): Promise; /** Collection the model uses. */ collection: Collection; /** Creates a `count` query: counts the number of documents that match `filter`. */ count(callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; count(filter: FilterQuery, callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */ countDocuments(filter: FilterQuery, options?: QueryOptions, callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; countDocuments(callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; /** Creates a new document or documents */ create>(docs: Array, options?: SaveOptions): Promise[]>; create>(docs: Array, callback: Callback[]>): void; create>(doc: DocContents | T): Promise>; create>(...docs: Array): Promise[]>; create>(doc: T | DocContents, callback: Callback>): void; /** * Create the collection for this model. By default, if no indexes are specified, * mongoose will not create the collection for the model until any documents are * created. Use this method to create the collection explicitly. */ createCollection(options: mongodb.CreateCollectionOptions & Pick | null, callback: Callback>): void; createCollection(callback: Callback>): void; createCollection(options?: mongodb.CreateCollectionOptions & Pick): Promise>; /** Connection the model uses. */ db: Connection; /** * Deletes all of the documents that match `conditions` from the collection. * Behaves like `remove()`, but deletes all documents that match `conditions` * regardless of the `single` option. */ deleteMany(filter?: FilterQuery, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; deleteMany(filter: FilterQuery, callback: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; deleteMany(callback: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; /** * Deletes the first document that matches `conditions` from the collection. * Behaves like `remove()`, but deletes at most one document regardless of the * `single` option. */ deleteOne(filter?: FilterQuery, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; deleteOne(filter: FilterQuery, callback: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; deleteOne(callback: CallbackWithoutResult): QueryWithHelpers, TQueryHelpers, T>; /** * Event emitter that reports any errors that occurred. Useful for global error * handling. */ events: NodeJS.EventEmitter; /** * Finds a single document by its _id field. `findById(id)` is almost* * equivalent to `findOne({ _id: id })`. If you want to query by a document's * `_id`, use `findById()` instead of `findOne()`. */ findById>( id: any, projection?: ProjectionType | null, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; findById>( id: any, projection?: ProjectionType | null, callback?: Callback ): QueryWithHelpers; /** Finds one document. */ findOne>( filter?: FilterQuery, projection?: ProjectionType | null, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; findOne>( filter?: FilterQuery, projection?: ProjectionType | null, callback?: Callback ): QueryWithHelpers; findOne>( filter?: FilterQuery, callback?: Callback ): QueryWithHelpers; /** * Shortcut for creating a new Document from existing raw data, pre-saved in the DB. * The document returned has no paths marked as modified initially. */ hydrate(obj: any, projection?: AnyObject, options?: { setters?: boolean }): HydratedDocument; /** * This function is responsible for building [indexes](https://docs.mongodb.com/manual/indexes/), * unless [`autoIndex`](http://mongoosejs.com/docs/guide.html#autoIndex) is turned off. * Mongoose calls this function automatically when a model is created using * [`mongoose.model()`](/docs/api.html#mongoose_Mongoose-model) or * [`connection.model()`](/docs/api.html#connection_Connection-model), so you * don't need to call it. */ init(callback?: CallbackWithoutResult): Promise>; /** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */ insertMany(docs: Array, options: InsertManyOptions & { lean: true; }, callback: Callback, Require_id>>>): void; insertMany(docs: Array, options: InsertManyOptions & { rawResult: true; }, callback: Callback>): void; insertMany(docs: Array, callback: Callback, Require_id>, TMethodsAndOverrides, TVirtuals>>>): void; insertMany(doc: DocContents, options: InsertManyOptions & { lean: true; }, callback: Callback, Require_id>>>): void; insertMany(doc: DocContents, options: InsertManyOptions & { rawResult: true; }, callback: Callback>): void; insertMany(doc: DocContents, options: InsertManyOptions & { lean?: false | undefined }, callback: Callback, Require_id>, TMethodsAndOverrides, TVirtuals>>>): void; insertMany(doc: DocContents, callback: Callback, Require_id>, TMethodsAndOverrides, TVirtuals>>>): void; insertMany(docs: Array, options: InsertManyOptions & { lean: true; }): Promise, Require_id>>>; insertMany(docs: Array, options: InsertManyOptions & { rawResult: true; }): Promise>; insertMany(docs: Array): Promise, Require_id>, TMethodsAndOverrides, TVirtuals>>>; insertMany(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise, Require_id>>>; insertMany(doc: DocContents, options: InsertManyOptions & { rawResult: true; }): Promise>; insertMany(doc: DocContents, options: InsertManyOptions): Promise, Require_id>, TMethodsAndOverrides, TVirtuals>>>; insertMany(doc: DocContents): Promise, Require_id>, TMethodsAndOverrides, TVirtuals>>>; /** The name of the model */ modelName: string; /** Populates document references. */ populate(docs: Array, options: PopulateOptions | Array | string, callback?: Callback<(HydratedDocument)[]>): Promise>>; populate(doc: any, options: PopulateOptions | Array | string, callback?: Callback>): Promise>; /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */ validate(callback?: CallbackWithoutResult): Promise; validate(optional: any, callback?: CallbackWithoutResult): Promise; validate(optional: any, pathsToValidate: PathsToValidate, callback?: CallbackWithoutResult): Promise; /** Watches the underlying collection for changes using [MongoDB change streams](https://docs.mongodb.com/manual/changeStreams/). */ watch(pipeline?: Array>, options?: mongodb.ChangeStreamOptions & { hydrate?: boolean }): mongodb.ChangeStream; /** Adds a `$where` clause to this query */ $where(argument: string | Function): QueryWithHelpers>, HydratedDocument, TQueryHelpers, T>; /** Registered discriminators for this model. */ discriminators: { [name: string]: Model } | undefined; /** Translate any aliases fields/conditions so the final query or document object is pure */ translateAliases(raw: any): any; /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */ distinct(field: string, filter?: FilterQuery, callback?: Callback): QueryWithHelpers, HydratedDocument, TQueryHelpers, T>; /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */ estimatedDocumentCount(options?: QueryOptions, callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; /** * Returns a document with its `_id` if at least one document exists in the database that matches * the given `filter`, and `null` otherwise. */ exists(filter: FilterQuery, callback: Callback<{ _id: InferId } | null>): QueryWithHelpers, '_id'> | null, HydratedDocument, TQueryHelpers, T>; exists(filter: FilterQuery): QueryWithHelpers<{ _id: InferId } | null, HydratedDocument, TQueryHelpers, T>; /** Creates a `find` query: gets a list of documents that match `filter`. */ find>( filter: FilterQuery, projection?: ProjectionType | null | undefined, options?: QueryOptions | null | undefined, callback?: Callback | undefined ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; find>( filter: FilterQuery, projection?: ProjectionType | null | undefined, callback?: Callback | undefined ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; find>( filter: FilterQuery, callback?: Callback | undefined ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; find>( callback?: Callback | undefined ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */ findByIdAndDelete>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */ findByIdAndRemove>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */ findByIdAndUpdate>(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; findByIdAndUpdate>(id: mongodb.ObjectId | any, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers; findByIdAndUpdate>(id?: mongodb.ObjectId | any, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; findByIdAndUpdate>(id: mongodb.ObjectId | any, update: UpdateQuery, callback: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */ findOneAndDelete>(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */ findOneAndRemove>(filter?: FilterQuery, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */ findOneAndReplace>(filter: FilterQuery, replacement: T | AnyObject, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; findOneAndReplace>(filter: FilterQuery, replacement: T | AnyObject, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers; findOneAndReplace>(filter?: FilterQuery, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers; /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */ findOneAndUpdate>( filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; findOneAndUpdate>( filter: FilterQuery, update: UpdateQuery, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void ): QueryWithHelpers; findOneAndUpdate>( filter?: FilterQuery, update?: UpdateQuery, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null, res: any) => void ): QueryWithHelpers; geoSearch>( filter?: FilterQuery, options?: GeoSearchOptions, callback?: Callback> ): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; /** Executes a mapReduce command. */ mapReduce( o: MapReduceOptions, callback?: Callback ): Promise; remove>(filter?: any, callback?: CallbackWithoutResult): QueryWithHelpers; remove>(filter?: any, options?: RemoveOptions, callback?: CallbackWithoutResult): QueryWithHelpers; /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */ replaceOne>( filter?: FilterQuery, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; /** Schema the model uses. */ schema: Schema; /** * @deprecated use `updateOne` or `updateMany` instead. * Creates a `update` query: updates one or many documents that match `filter` with `update`, based on the `multi` option. */ update>( filter?: FilterQuery, update?: UpdateQuery | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */ updateMany>( filter?: FilterQuery, update?: UpdateQuery | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */ updateOne>( filter?: FilterQuery, update?: UpdateQuery | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback ): QueryWithHelpers; /** Creates a Query, applies the passed conditions, and returns the Query. */ where>(path: string, val?: any): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; where>(obj: object): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; where>(): QueryWithHelpers, ResultDoc, TQueryHelpers, T>; } }