var Schema = new Schema({
time: {
type: Date,
default: Date.now(),
expires: '300' //string,以秒为单位
}
})
非mongoose版:
db.collection_name.createIndex({"time":1}, {expireAfterSeconds: 10}) //以秒为单位
注意:
- 存储在过期索引字段的值必须是指定的时间类型(必须是 ISODate 或 ISODate 数组,不能使用时间戳,否则不能被自动删除)
- 如果指定了 ISODate 数组,则按照最小的时间进行删除
- 过期索引不能是复合索引(不能指定两个过期时间)
- 删除时间存在些许误差(1 分钟左右)