javascript - Mongoose doesn't create TTL indexes - Stack Overflow

This is my Mongoose model:var sessionSchema = new Schema({_id: { type: String, required: true, index:

This is my Mongoose model:

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)

I need to be able to set a date object into expire (usually it's something like Date.now plus a few minutes) and have the object removed from the collection after 6 hours past the expiration.

However, I'm not able to have Mongoose to create the index. When I run db.sessions.getIndexes() in the mongo console, here's the output:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "dev.sessions"
    }
]

I've tried also with different syntaxes, like
expire: { type: Date, expires: 21600 } (Mongoose's short-hand version).
I tried also defining the index at the schema level:
sessionSchema.index({ expire: 1 }, { expireAfterSeconds: 21600 })
None is working.

Unlike others who asked questions on SO, my index is simply not created. I've tried also removing the collection and the database as well, and when they're recreated they still don't contain the index.

Versions: Mongoose 3.8.19, MongoDB 2.6.5 (OSX) and Node.js 0.10.33

Edit

More info: I tried creating the index directly from the mongo console, with:
db.sessions.ensureIndex({"expire":1}, {expireAfterSeconds: 21600})
That appears to be working (the index is created).

However, it's not working with Mongoose in any way.

This is my Mongoose model:

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)

I need to be able to set a date object into expire (usually it's something like Date.now plus a few minutes) and have the object removed from the collection after 6 hours past the expiration.

However, I'm not able to have Mongoose to create the index. When I run db.sessions.getIndexes() in the mongo console, here's the output:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "dev.sessions"
    }
]

I've tried also with different syntaxes, like
expire: { type: Date, expires: 21600 } (Mongoose's short-hand version).
I tried also defining the index at the schema level:
sessionSchema.index({ expire: 1 }, { expireAfterSeconds: 21600 })
None is working.

Unlike others who asked questions on SO, my index is simply not created. I've tried also removing the collection and the database as well, and when they're recreated they still don't contain the index.

Versions: Mongoose 3.8.19, MongoDB 2.6.5 (OSX) and Node.js 0.10.33

Edit

More info: I tried creating the index directly from the mongo console, with:
db.sessions.ensureIndex({"expire":1}, {expireAfterSeconds: 21600})
That appears to be working (the index is created).

However, it's not working with Mongoose in any way.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 12, 2014 at 17:13 ItalyPaleAleItalyPaleAle 7,3406 gold badges48 silver badges74 bronze badges 2
  • I've a feeling this is a bug with Mongoose. Filed github./LearnBoost/mongoose/issues/2459 – ItalyPaleAle Commented Nov 12, 2014 at 18:29
  • I am actually interested in knowing if this works, running into the same issue. – Lion789 Commented Nov 14, 2014 at 22:41
Add a ment  | 

1 Answer 1

Reset to default 4

Apparently the problem was that I created an index on the custom _id field. MongoDB creates an index on that field by itself, so when Mongoose was calling ensureIndex to create also the TTL index, it failed for both.

See https://github./LearnBoost/mongoose/issues/2459

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745157105a4614188.html

相关推荐

  • javascript - Mongoose doesn't create TTL indexes - Stack Overflow

    This is my Mongoose model:var sessionSchema = new Schema({_id: { type: String, required: true, index:

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信