Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Not Creating Indexes

Trying to create MongoDB indexes. Using the Mongoose ODM and in my schema definition below I have the username field set to a unique index. The collection and document all get created properly, it's just the indexes that aren't working. All the documentation says that the ensureIndex command should be run at startup to create any indexes, but none are being made. I'm using MongoLab for hosting if that matters. I have also repeatedly dropped the collection. What is wrong.

var schemaUser = new mongoose.Schema({
    username: {type: String, index: { unique: true }, required: true},
    hash: String,
    created: {type: Date, default: Date.now}
}, { collection:'Users' });

var User = mongoose.model('Users', schemaUser);
var newUser = new Users({username:'wintzer'})
newUser.save(function(err) {
    if (err) console.log(err);
});
like image 980
wintzer Avatar asked Sep 17 '12 03:09

wintzer


4 Answers

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.

like image 198
JohnnyHK Avatar answered Oct 12 '22 08:10

JohnnyHK


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.

like image 37
joeytwiddle Avatar answered Oct 12 '22 08:10

joeytwiddle


In my case I had to explicitly specify autoIndex: true:

const User = new mongoose.Schema({
    name: String,
    email: String,
    password: String
 },
 {autoIndex: true}
)
like image 5
DannyMoshe Avatar answered Oct 12 '22 10:10

DannyMoshe


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.

like image 2
Newclique Avatar answered Oct 12 '22 10:10

Newclique