diff --git a/lib/model.js b/lib/model.js index 922360862b9..fb71f9ebc23 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1529,6 +1529,50 @@ Model.createSearchIndex = async function createSearchIndex(description) { return await this.$__collection.createSearchIndex(description); }; +/** + * Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/). + * This function only works when connected to MongoDB Atlas. + * + * #### Example: + * + * const schema = new Schema({ name: { type: String, unique: true } }); + * const Customer = mongoose.model('Customer', schema); + * await Customer.updateSearchIndex('test', { mappings: { dynamic: true } }); + * + * @param {String} name + * @param {Object} definition + * @return {Promise} + * @api public + */ + +Model.updateSearchIndex = async function updateSearchIndex(name, definition) { + _checkContext(this, 'updateSearchIndex'); + + return await this.$__collection.updateSearchIndex(name, definition); +}; + +/** + * Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name. + * This function only works when connected to MongoDB Atlas. + * + * #### Example: + * + * const schema = new Schema({ name: { type: String, unique: true } }); + * const Customer = mongoose.model('Customer', schema); + * await Customer.dropSearchIndex('test'); + * + * @param {String} name + * @param {Object} definition + * @return {Promise} + * @api public + */ + +Model.dropSearchIndex = async function dropSearchIndex(name) { + _checkContext(this, 'dropSearchIndex'); + + return await this.$__collection.dropSearchIndex(name); +}; + /** * Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`. * diff --git a/types/models.d.ts b/types/models.d.ts index 22362d5dcde..946121823fc 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -300,6 +300,8 @@ declare module 'mongoose' { 'deleteOne' >; + dropSearchIndex(name: string): Promise; + /** * Event emitter that reports any errors that occurred. Useful for global error * handling. @@ -475,6 +477,8 @@ declare module 'mongoose' { doc: any, options: PopulateOptions | Array | string ): Promise>; + updateSearchIndex(name: string, definition: AnyObject): Promise; + /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */ validate(): Promise; validate(obj: any): Promise;