diff --git a/lib/model.js b/lib/model.js index 1e2729320d7..477a14c76b9 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1655,6 +1655,31 @@ Model.dropSearchIndex = async function dropSearchIndex(name) { return await this.$__collection.dropSearchIndex(name); }; +/** + * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection. + * 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.createSearchIndex({ name: 'test', definition: { mappings: { dynamic: true } } }); + * const res = await Customer.listSearchIndexes(); // Includes `[{ name: 'test' }]` + * + * @param {Object} [options] + * @return {Promise} + * @api public + */ + +Model.listSearchIndexes = async function listSearchIndexes(options) { + _checkContext(this, 'listSearchIndexes'); + + const cursor = await this.$__collection.listSearchIndexes(options); + + return await cursor.toArray(); +}; + /** * 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 a56f1ee92e4..aa207f6b45b 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -544,6 +544,12 @@ declare module 'mongoose' { Array>> >; + /** + * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection. + * This function only works when connected to MongoDB Atlas. + */ + listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise>; + /** The name of the model */ modelName: string;