如何在Mongoose中重置文档的到期日期? [英] How do I reset the expiration date of a document in Mongoose?

查看:104
本文介绍了如何在Mongoose中重置文档的到期日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此帖子中的答案我创建了以下文档架构,该文档架构将创建的每个新文档设置为在创建后24小时过期:

Based on the answer in this post I have created the following document schema, which sets every new document created to expire 24 hours after its creation :

var mongoose = require('./node_modules/mongoose');
mongoose.connect(mongodburi, {
    server : {
        socketOptions : {
            keepAlive: 1
        }
    },
    replset : {
        socketOptions : {
            keepAlive: 1
        }
    }
});
var sessionSchema = mongoose.Schema({
    uid: {
        type: String,
        required: true,
        unique: true
    },
    token: {
        type: String,
        required: false,
        unique: true
    },
    createdAt: { 
        type: Date, 
        default: Date.now,
        expires: 24*60*60
    }
});

var Session = mongoose.model('Session', sessionSchema);

我希望能够将文档的有效期重新设置24小时.这是这样做的方法吗(?):

I want to be able to reset the expiration of a document for another 24 hours. Is this the way to do it (?) :

Session.update({uid: someUID}, {createdAt: Date.now}, null, function (err, numOfSessionsUpdated)
{
   if (numOfSessionsUpdated > 0)
   {
      console.log('session expiration has been postponed for another 24 hours');
   }    
});

推荐答案

这很接近,但是您需要调用 Date.now而不是仅仅通过它,因为它是一个函数:

That's close, but you need to call Date.now instead of just passing it as that's a function:

Session.update({uid: someUID}, {createdAt: Date.now()}, null, function (err, numOfSessionsUpdated)
{
   if (numOfSessionsUpdated > 0)
   {
      console.log('session expiration has been postponed for another 24 hours');
   }    
});

这篇关于如何在Mongoose中重置文档的到期日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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