I have created the following document schema :
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
}
});
var Session = mongoose.model('Session', sessionSchema);
I use the above to create/add documents to my Mongo DB like so :
var session = new Session();
session.uid = getUniqueID();
session.token = getToken();
session.save(function (err, session)
{
if (session.uid && session.uid.length) console.log('new doc saved');
else console.log('failed to save new doc');
});
Whenever I create a document I want it to automatically expire (i.e. get deleted from the database) 24 hours after the moment it was created.
How do I do that ?
I found a somewhat relevant post in SO but apparently it's about expiring collections whereas I'm interested in expiring documents, not collections of documents.
If this question has indeed already been answered by the above post please feel free to delete it to avoid clutter. I have created a new question about resetting the expiration date of a document (to which I found no answer on SO)