Skip to content

Commit

Permalink
Merge pull request #14519 from Automattic/IslandRhythms/gh-14450
Browse files Browse the repository at this point in the history
Add `Model.listSearchIndexes()`
  • Loading branch information
vkarpov15 committed Apr 22, 2024
2 parents 964fa85 + ec518ed commit d509ae8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array>}
* @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()`.
*
Expand Down
6 changes: 6 additions & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ declare module 'mongoose' {
Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>
>;

/**
* 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<Array<{ name: string }>>;

/** The name of the model */
modelName: string;

Expand Down

0 comments on commit d509ae8

Please sign in to comment.