Mongoose, creating new indexes on existing cluster?

We have an existing mongo cluster on mongodb.com, which we are using with a nodejs server, by means of mongoose.

Over time, we realised that we needed to add indexes to a number of fields and now we have added ‘text’ indexes. When we do a search we running into the error that the field we are searching on need to be indexed as text, but it was set up as such in mongoose and local testing worked, but not since using our dev DB in mongo cloud.

Looking in the admin panel of out cluster and in the indexes for the collection we do see it is not created, but we aren’t sure why. We did manually remove the indexes from the question in collection and restarted our nodejs server, but no change.

We are able to create all the indexes manually in Mongo Cloud, but I would rather avoid any manual steps if possible.

Can anyone indicate how I can get the fields to be indexed, if they weren’t marked as being indexed before?

Follows is a reduced sample of the schema definition we are using (code is in Typescript):

    interface IStoreDB extends IStore, Document {}

    const storeSchema = new mongoose.Schema({
        name:{
            type: String,
            required:true,
            index: true
        },
        mobile: {
            type: String,
            required: false,
            unique: true,
            sparse: true,
            index: true
        },

    }, {
        timestamps: true
    });

    storeSchema.index({ name: 'text', mobile: 'text' });

    const model = createModel<IStoreDB>('Store', storeSchema);

    export default model;

versions:

  • mongoose@5.9.5
  • mongodb@3.5.5

The code you mention here is capable of creating indexes for new products you are going to insert. To create indexes for existing documents in your cluster you need to manually call “createIndexes”. Create an API to call “createIndexes” in all your existing documents.