Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,7 @@ Model.init = function init() {
return;
}

const results = [];
for (const searchIndex of this.schema._searchIndexes) {
results.push(await this.createSearchIndex(searchIndex));
}
return results;
return await this.ensureSearchIndexes();
};
const _createCollection = async() => {
let autoCreate = utils.getOption(
Expand Down Expand Up @@ -1792,6 +1788,35 @@ function _ensureIndexes(model, options, callback) {
}
}

/**
* Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema.
* This function only works when connected to MongoDB Atlas.
*
* #### Example:
*
* const schema = new Schema({
* name: String,
* description: String
* });
* schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
* const Product = mongoose.model('Product', schema);
*
* // Creates the search index defined in the schema
* await Product.createSearchIndexes();
*
* @api public
* @return {Promise} resolves to the results of creating the search indexes
*/

Model.createSearchIndexes = async function createSearchIndexes() {
_checkContext(this, 'createSearchIndexes');
const results = [];
for (const searchIndex of this.schema._searchIndexes) {
results.push(await this.createSearchIndex(searchIndex));
}
return results;
};

/**
* Schema the model uses.
*
Expand Down
31 changes: 29 additions & 2 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* Test dependencies.
*/
const sinon = require('sinon');
const start = require('./common');

const CastError = require('../lib/error/cast');
const assert = require('assert');
const model = require('../lib/model');
const { once } = require('events');
const random = require('./util').random;
const util = require('./util');
const model = require('../lib/model');
const sinon = require('sinon');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;
Expand Down Expand Up @@ -8686,6 +8686,33 @@ describe('Model', function() {
assert.equal(doc.name, undefined);
});
});

it('createSearchIndexes creates an index for each search index in schema (gh-15465)', async function() {
const sinon = require('sinon');
const schema = new mongoose.Schema({
name: String,
description: String
});

schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });

const TestModel = db.model('Test', schema);

const createSearchIndexStub = sinon.stub(TestModel, 'createSearchIndex').resolves({ acknowledged: true });

try {
const results = await TestModel.createSearchIndexes();

assert.equal(createSearchIndexStub.callCount, 1);
assert.equal(results.length, 1);
assert.deepEqual(results, [{ acknowledged: true }]);

// Verify that createSearchIndex was called with the correct arguments
assert.ok(createSearchIndexStub.firstCall.calledWithMatch({ name: 'test', definition: { mappings: { dynamic: true } } }));
} finally {
sinon.restore();
}
});
});


Expand Down
6 changes: 6 additions & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ declare module 'mongoose' {
*/
createSearchIndex(description: SearchIndexDescription): Promise<string>;

/**
* Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema.
* This function only works when connected to MongoDB Atlas.
*/
createSearchIndexes(): Promise<string[]>;

/** Connection the model uses. */
db: Connection;

Expand Down