Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTL indexes are not created #2459

Closed
ItalyPaleAle opened this issue Nov 12, 2014 · 15 comments
Closed

TTL indexes are not created #2459

ItalyPaleAle opened this issue Nov 12, 2014 · 15 comments

Comments

@ItalyPaleAle
Copy link

With Mongoose 3.8.19 and MongoDB 2.6.5, Mongoose is not creating TTL indexes ("expireAfterSeconds"), in no way.

See this SO question: http://stackoverflow.com/questions/26892660/mongoose-doesnt-create-ttl-indexes

@shrayolacrayon shrayolacrayon self-assigned this Nov 12, 2014
@shrayolacrayon
Copy link
Contributor

You can create the index in this sort of manner -

exampleSchema = {field1 : {type : Date, index: {expires : 50}}, field2: String}

@ItalyPaleAle
Copy link
Author

Thanks Shraya.
As I wrote on SO, I've tried that too (and also expires with the final s). None of those work, hence I suspect a bug in Mongoose.

@shrayolacrayon
Copy link
Contributor

So this may be a workaround, but after creating the schema with the defined indexes and the model, can you try using ModelName.ensureIndexes?
See http://mongoosejs.com/docs/api.html#model_Model.ensureIndexes.

You will be able to get the same functionality as the ensureIndexes() command in the mongo shell.
Let me know if that works.

@ItalyPaleAle
Copy link
Author

It's still not working:

'use strict';

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var sessionSchema = new Schema({
    _id: { type: String, required: true, index: { unique: true } },
    user: { type: Schema.Types.ObjectId },
    expire: { type: Date, index: { expireAfterSeconds: 21600 } }
})

module.exports = mongoose.model('Session', sessionSchema)

// Fix for Mongoose not creating the TTL indexes (see https://github.com/LearnBoost/mongoose/issues/2459#issuecomment-62802096 )
mongoose.model('Session').ensureIndexes(function(err) {
    console.log('ensure index', err)
})

On the console I get:

MongoError: Index with name: _id_ already exists with different options

(Note I had deleted the collection prior restarting the Node app)

@shrayolacrayon
Copy link
Contributor

Okay, so it seems that your real problem is not with the TTL index, but with the _id index, which is created automatically, and in turn is failing at the start of creating the indexes.
I'll look into a potential solution regarding non-default _ids in Mongoose.

@ItalyPaleAle
Copy link
Author

Ok, I'm not on my work computer now so I can't test this assumption... But given that (a) the index on _id is created (I can see it in the database, even if it doesn't look like it's set as "unique", as I specified) and that (b) ensureIndex with Mongoose is failing; I assume that something is creating an index by default on the custom _id. Is that MongoDB or Mongoose? If it's Mongoose, why is it doing that?

@shrayolacrayon
Copy link
Contributor

MongoDB by default creates a unique index on _id so you need not create an index on it. You also do not need to include the ensureIndex call. Your schema should work just fine if you take out the index field in _id.

@ItalyPaleAle
Copy link
Author

Ok, I removed the index on the _id field and it's now working.
Thanks for your help.

PS: Maybe you should consider specifying it in the docs?

@arimus
Copy link

arimus commented Nov 24, 2014

This was fun to track down. However, I was indeed simply able to remove the unique:true from two of the_id fields in my schema definitions. Then there was no error and the rest of my indexes were created fine.

Having this error logged in the console (at very least in debug mode), since it is a severe error in that indexes will not be generated, would seem to be a good enhancement. It's good to know when expected behavior is failing & what the problem is.

@vkarpov15
Copy link
Collaborator

Models have an "index" event that's emitted when index builds are done, or when an index fails to build. See http://stackoverflow.com/questions/14333603/how-should-mongooses-model-onindex-be-used

@dman777
Copy link

dman777 commented Oct 12, 2015

Would it be the same thing here with my index: true? I can't get expires to work:

var CpuSchema = new Schema({
    createdAt: { type: Date, expires: '24h' },
    timestamp : { type : Date, index: true },

@vkarpov15
Copy link
Collaborator

Do schema.index({ createdAt: 1 }, { expireAfterSeconds: 3600 });. http://docs.mongodb.org/manual/core/index-ttl/

@dman777
Copy link

dman777 commented Oct 13, 2015

Isn't mongoose supposed to take care of this? I would like to know if my timestamp : { type : Date, index: true } is interfering with mongoose creating the ttl index.

@vkarpov15
Copy link
Collaborator

Try listening to the Model.on('index', function(error) {}); event to see if there are any issues building indexes.

Also, is mongoose supposed to take care of what exactly?

@revstage
Copy link

revstage commented Dec 16, 2019

this schema configuration works for me on Mongoose 5.7.0 and MongoDB v4.2.0

expireAt: {
  type: Date,
  default: Date.now,
  index: { expires: '1d' }
}

@Automattic Automattic locked as resolved and limited conversation to collaborators Dec 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants