Expires setting in mongoose schema is ignored if index already exists at MongoDB Expires setting in mongoose schema is ignored if index already exists at MongoDB mongoose mongoose

Expires setting in mongoose schema is ignored if index already exists at MongoDB


You cannot do it from schema definition.

The best way is to drop index directly in the db.

Alternatively, drop it from the script. E.g.

mongoose.connections[0].collections("collectionname").dropIndex("propertyName",callback)


Thanks to Alex Blex hint, I have finally solved this. First droping the index, then recreating with the new expiration value:

mongoose.connections[0].collections.crumbs.dropIndex({timestamp: 1}, function (error) {    if (error) {        // This happens typically if the index doesn't exist but it doesn't hurt anyway    }    mongoose.connections[0].collections.crumbs.ensureIndex({timestamp: 1}, {expireAfterSeconds: 360});});