Javascript 猫鼬不';不创建TTL索引
这是我的猫鼬模型:Javascript 猫鼬不';不创建TTL索引,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,这是我的猫鼬模型: 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.mo
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)
我需要能够将date对象设置为expire(通常是date.now
加上几分钟),并在过期6小时后将该对象从集合中删除
但是,我无法让Mongoose创建索引。在mongo控制台中运行db.sessions.getIndexes()
时,输出如下:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "dev.sessions"
}
]
我也尝试过使用不同的语法,比如expire:{type:Date,expires:21600}
(猫鼬的速记版)。我还尝试在模式级别定义索引:
sessionSchema.index({expire:1},{expireAfterSeconds:21600})
没有人在工作 与其他人提出的问题不同,我的索引并没有创建。我也尝试过删除集合和数据库,当它们被重新创建时,它们仍然不包含索引 版本:Mongoose 3.8.19、MongoDB 2.6.5(OSX)和Node.js 0.10.33 编辑 更多信息:我尝试直接从mongo控制台创建索引,使用:
db.sessions.ensureIndex({“expire”:1},{expireAfterSeconds:21600})
似乎正在工作(索引已创建)
但是,它对猫鼬没有任何作用。显然,问题是我在自定义
\u id
字段上创建了一个索引。MongoDB自己在该字段上创建索引,因此当Mongoose调用ensureIndex
来创建TTL索引时,这两个索引都失败了
看我觉得这是猫鼬的一个错误。事实上,我很想知道这是否有效,遇到了同样的问题。