From d5d1ad8f77cf71f819288b188bd5c0b6b6731f1d Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:08:52 -0400 Subject: [PATCH 1/3] Update model.js --- lib/model.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/model.js b/lib/model.js index 1e2729320d7..04b10ba700e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1655,6 +1655,15 @@ Model.dropSearchIndex = async function dropSearchIndex(name) { return await this.$__collection.dropSearchIndex(name); }; + +Model.listSearchIndexes = async function listSearchIndexes() { + _checkContext(this, 'listSearchIndexes'); + + const cursor = await this.$__collection.listSearchIndexes(); + + 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()`. * From 660562903422f48815f8edcde6c07168d3f01a61 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:20:45 -0400 Subject: [PATCH 2/3] fix: lint --- lib/model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 04b10ba700e..ba3f7e36492 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1662,7 +1662,7 @@ Model.listSearchIndexes = async function listSearchIndexes() { const cursor = await this.$__collection.listSearchIndexes(); 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()`. From ec518edbcb396b5558162602120d81e9e140c23d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 16 Apr 2024 15:33:18 -0400 Subject: [PATCH 3/3] add TypeScript types, jsdoc, and options support for Model.listSearchIndexes() --- lib/model.js | 20 ++++++++++++++++++-- types/models.d.ts | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/model.js b/lib/model.js index ba3f7e36492..477a14c76b9 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1655,11 +1655,27 @@ 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() { +Model.listSearchIndexes = async function listSearchIndexes(options) { _checkContext(this, 'listSearchIndexes'); - const cursor = await this.$__collection.listSearchIndexes(); + const cursor = await this.$__collection.listSearchIndexes(options); return await cursor.toArray(); }; 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;