Skip to content

Commit d509ae8

Browse files
authored
Merge pull request #14519 from Automattic/IslandRhythms/gh-14450
Add `Model.listSearchIndexes()`
2 parents 964fa85 + ec518ed commit d509ae8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/model.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,31 @@ Model.dropSearchIndex = async function dropSearchIndex(name) {
16551655
return await this.$__collection.dropSearchIndex(name);
16561656
};
16571657

1658+
/**
1659+
* List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
1660+
* This function only works when connected to MongoDB Atlas.
1661+
*
1662+
* #### Example:
1663+
*
1664+
* const schema = new Schema({ name: { type: String, unique: true } });
1665+
* const Customer = mongoose.model('Customer', schema);
1666+
*
1667+
* await Customer.createSearchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
1668+
* const res = await Customer.listSearchIndexes(); // Includes `[{ name: 'test' }]`
1669+
*
1670+
* @param {Object} [options]
1671+
* @return {Promise<Array>}
1672+
* @api public
1673+
*/
1674+
1675+
Model.listSearchIndexes = async function listSearchIndexes(options) {
1676+
_checkContext(this, 'listSearchIndexes');
1677+
1678+
const cursor = await this.$__collection.listSearchIndexes(options);
1679+
1680+
return await cursor.toArray();
1681+
};
1682+
16581683
/**
16591684
* Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
16601685
*

types/models.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ declare module 'mongoose' {
544544
Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>
545545
>;
546546

547+
/**
548+
* List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
549+
* This function only works when connected to MongoDB Atlas.
550+
*/
551+
listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise<Array<{ name: string }>>;
552+
547553
/** The name of the model */
548554
modelName: string;
549555

0 commit comments

Comments
 (0)