0

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);

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');
   }    
});
Community
  • 1
  • 1
Kawd
  • 4,122
  • 10
  • 37
  • 68

1 Answers1

1

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');
   }    
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • I see, thanks!. I was confused because in the Schema constructor you don't call that function (for the `createdAt` key), but I assume that's because, in the schema constructor, you simply indicate what function needs to be called when the time comes for a new doc to be created. – Kawd Nov 21 '15 at 15:42