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
Comments
You can create the index in this sort of manner -
|
Thanks Shraya. |
So this may be a workaround, but after creating the schema with the defined indexes and the model, can you try using You will be able to get the same functionality as the ensureIndexes() command in the mongo shell. |
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:
(Note I had deleted the collection prior restarting the Node app) |
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. |
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) |
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 |
Ok, I removed the index on the _id field and it's now working. PS: Maybe you should consider specifying it in the docs? |
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. |
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 |
Would it be the same thing here with my var CpuSchema = new Schema({
createdAt: { type: Date, expires: '24h' },
timestamp : { type : Date, index: true }, |
Do |
Isn't mongoose supposed to take care of this? I would like to know if my |
Try listening to the Also, is mongoose supposed to take care of what exactly? |
this schema configuration works for me on Mongoose 5.7.0 and MongoDB v4.2.0
|
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
The text was updated successfully, but these errors were encountered: