用于用户保存的令牌的 Mongoose TTL
Mongoose TTL for user saved tokens
我有这个用户模式。
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 给定时间。
有什么建议吗?
mongodb 无法做到这一点。
您可以查看此文档:MongoDb Index TTL
TTL indexes are special single-field indexes that MongoDB can use to
automatically remove documents from a collection after a certain
amount of time or at a specific clock time. Data expiration is useful
for certain types of information like machine generated event data,
logs, and session information that only need to persist in a database
for a finite amount of time.
您可以为用户令牌创建一个新集合并将它们关联到模型上。我认为这可能是一种更好的做法。然后你可以在令牌集合上使用 ttl 索引
我有这个用户模式。
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 给定时间。
有什么建议吗?
mongodb 无法做到这一点。 您可以查看此文档:MongoDb Index TTL
TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time. Data expiration is useful for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time.
您可以为用户令牌创建一个新集合并将它们关联到模型上。我认为这可能是一种更好的做法。然后你可以在令牌集合上使用 ttl 索引