0

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)

Community
  • 1
  • 1
Kawd
  • 4,122
  • 10
  • 37
  • 68
  • 1
    The answer you linked to is what you want. You'd add a `createdAt` field to your schema to support it the expiration of your docs. – JohnnyHK Nov 21 '15 at 14:09
  • And this will only expire that document and not the whole collection, right ? – Kawd Nov 21 '15 at 14:28
  • 1
    Yep, documents are individually expired based on the `createdAt` value in each document. – JohnnyHK Nov 21 '15 at 15:07
  • Thanks! Any thoughts on this : http://stackoverflow.com/questions/33844643/how-do-i-reset-the-expiration-date-of-a-document-in-mongoose-node-js ? – Kawd Nov 21 '15 at 15:19

0 Answers0