Mongoose Not Creating Indexes

Solution 1:

Hook the 'index' event on the model to see if any errors are occurring when asynchronously creating the index:

User.on('index', function(err) {
    if (err) {
        console.error('User index error: %s', err);
    } else {
        console.info('User indexing complete');
    }
});

Also, enable Mongoose's debug logging by calling:

mongoose.set('debug', true);

The debug logging will show you the ensureIndex call it's making for you to create the index.

Solution 2:

Mongoose declares "if the index already exists on the db, it will not be replaced" (credit).

For example if you had previous defined the index {unique: true} but you want to change it to {unique: true, sparse: true} then unfortunately Mongoose simply won't do it, because an index already exists for that field in the DB.

In such situations, you can drop your existing index and then mongoose will create a new index from fresh:

$ mongo
> use MyDB
> db.myCollection.dropIndexes();
> exit
$ restart node app

Beware that this is a heavy operation so be cautious on production systems!


In a different situation, my indexes were not being created, so I used the error reporting technique recommended by JohnnyHK. When I did that I got the following response:

E11000 duplicate key error collection

This was because my new index was adding the constraint unique: true but there were existing documents in the collection which were not unique, so Mongo could not create the index.

In this situation, I either need to fix or remove the documents with duplicate fields, before trying again to create the index.

Solution 3:

It might be solve your problem

var schema = mongoose.Schema({
speed: Number,
watchDate: Number,
meterReading: Number,
status: Number,
openTrack: Boolean,
 });
schema.index({ openTrack: 1 });

Solution 4:

When I hooked the index event on the model that wasn't working, I started getting an error on the console that indicated "The field 'retryWrites' is not valid for an index specification." The only place in my app that referenced 'retryWrites' was at the end of my connection string. I removed this, restarted the app, and the index rebuild was successful. I put retryWrites back in place, restarted the app, and the errors were gone. My Users collection (which had been giving me problems) was empty so when I used Postman to make a new record, I saw (with Mongo Compass Community) the new record created and the indexes now appear. I don't know what retryWrites does - and today was the first day I used it - but it seemed to be at the root of my issues.

Oh, and why did I use it? It was tacked onto a connection string I pulled from Mongo's Atlas Cloud site. It looked important. Hmm.