【问题标题】:Mongoose TTL for user saved tokens用户保存令牌的 Mongoose TTL
【发布时间】:2021-04-03 21:36:00
【问题描述】:

我有这个用户架构。

const userSchema = new Schema({
name: {
    type: String,
    required: true,
},
email: {
    type: String,
    required: true,
    unique: true
},
password: {
    type: String,
    required: true
},
tokens: [{
    token: {
        type: String,
        required: true,
    }
}], {timestamps: true});

我希望提供的令牌将在给定的时间expiresAt

有什么建议吗?

【问题讨论】:

标签: node.js mongodb mongoose schema ttl


【解决方案1】:

这对于 mongodb 是不可能的。 你可以查看这个文档:MongoDb Index TTL

TTL 索引是 MongoDB 可以使用的特殊单字段索引 在某个特定时间后自动从集合中删除文档 时间量或特定时钟时间。数据过期很有用 对于某些类型的信息,例如机器生成的事件数据, 日志和会话信息只需要保存在数据库中 在有限的时间内。

您可以为用户令牌创建一个新集合并将它们与模型相关联。我认为这可能是一个更好的做法。然后你可以在令牌集合上使用 ttl 索引

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多