猫鼬使数据过期但保留在数据库中 [英] Mongoose expire data but keep in database

查看:53
本文介绍了猫鼬使数据过期但保留在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过像这样的猫鼬模式保存数据并从数据库中过期/过期:

I currently have data saving and expiring to/from a database via a mongoose schema like so:

var UserSchema = new Schema({
    createdAt: { type: Date, expires: '1m' },
    name: String,
    email: String
});

唯一的问题是,已保存到数据库的文档已从数据库中完全删除.我将如何重构上述内容,以便名称/电子邮件地址保留在数据库中,但是如果用户在到期日期后尝试登录,则会收到一条消息,提示会话已过期,请重新连接会话". (或类似的东西)

The only problem is that the document that's saved to the database is completely removed from the database. How would I refactor the above so that the name/email address stay in the database but if the user attempts to login after their expiry date then they're greeted with a message saying 'session has expired, renew session'. (or something similar)

我要这样做,因为如果用户使用过期的电子邮件地址登录,服务器仍然可以查找该电子邮件地址并吐出会话已过期"消息,而不是未找到"消息错误,即删除数据时发生的错误.

I'm wanting to do it this way because then if the user logs in with an expired email address the server is still able to lookup the email address and spit out a "expired session" message rather than a "not found" error which is what happens when data is deleted.

重申一下,我如何将过期数据保存在mongo/mongoose数据库中,以便该应用程序能够找到用户尝试登录的电子邮件地址,但是如果他们的会话已过期,则需要更新该会话? /p>

So to reiterate, how do I keep expired data in a mongo/mongoose database so the app is able to find the email address the user is attempting to login with but if their session has expired they need to renew the session?

推荐答案

为此,您应该使用 Schema Reference 的概念.将您的过期字段保存在另一张表中,并连接主user_tableexpire_table(wxample名称)

You should use concept of Schema Reference for this. Save your expired field in another table and join your main user_table and expire_table(wxample name)

var UserSchema = new Schema({
    name: String,
    email: String
});

//save date by-default
//expire in 1 min as in your example
var expireSchema = new Schema({
    createdAt: { type: Date, default: Date.now, expires: '1m'  },
    user_pk: { type: Schema.Types.ObjectId, ref: 'user_expire'}
});

var userTable = mongoose.model('user_expire', UserSchema);
var expireTable = mongoose.model('expireMe', expireSchema);

//Save new user
var newUser =  new userTable({
    name: 'my_name',
    email: 'my_email'
});

newUser.save(function(err, result) {
    console.log(result, 'saved')
    var newExpire =  new expireTable({
        user_pk:result._id
    });
    //use _id of new user and save it to expire table
    newExpire.save(function(err, result) {
        console.log('saved relation')
    })
})

现在可以检测会话是否已过期

1.在数据过期之前执行此代码的方法

expireTable.findOne()
.populate('user_pk')
.exec(function (err, result) {
    if (err) throw err;
    console.log(result)
    if(result == null) {
        console.log('session has expired, renew session')
    } else {
        console.log('session is active')
    }
});

//output - session is active

2.在数据过期后执行此代码的问题

expireTable.findOne()
.populate('user_pk')
.exec(function (err, result) {
    if (err) throw err;
    console.log(result)
    if(result == null) {
        console.log('session has expired, renew session')
    } else {
        console.log('session is active')
    }
});

//output - session has expired, renew session

这篇关于猫鼬使数据过期但保留在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆