node.js - Time to live in mongodb, mongoose dont work. Documents doesnt get deleted -


im using scheme session in node.js app

var mongoose     = require('mongoose'); var schema       = mongoose.schema; // define schema our user session model var usersessionschema   = new schema({     sessionactivity:    { type: date, expires: '15s' }, // expire after 15 s     user_token:         { type: string, required: true } }); module.exports = mongoose.model('usersession', usersessionschema); 

and create "session" in app with:

... var session = new session(); session.user_token = profile.token; session.save(function(save_err) {     if (save_err) {         ....     } else {         // store session id in profile         profile.session_key = session._id;         profile.save(function(save_err, profile) {             if (save_err) {                 ...             } else {                 res.json({ status: 'ok', session_id: profile.session_id });             } }); ... 

the problem document lives permanetly, never expires. should live 15 seconds (up minute). whats wrong code? have tried set expries: string number i.e 15, string '15s' , on.

var usersessionschema   = new schema({     sessionactivity:    { type: date, expires: '15s', default: date.now }, // expire after 15 s     user_token:         { type: string, required: true } }); 

a ttl index deletes document 'x' seconds after value (which should date or array of dates) has passed. ttl checked every minute, may live little longer given 15 seconds.

to give date default value, can use default option in mongoose. accepts function. in case, date() returns current timestamp. set date current time once.

you go route:

usersessionschema.pre("save", function(next) {      this.sessionactivity = new date();      next();  }); 

this update value every time call .save() (but not .update()).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -