Reputation: 2164
I am new to mongodb and mongoose, and I am having a hard time figuring out how to do the following. I have a Schema, say "User", which includes a field like this:
createdAt : {type: Date, required: true, default: Date.now, expires: '4h'}
Therefore, when I create a new user, say var user=new User()
(assume User requires
the correct model), the new user created will be deleted after (approximately) 4h (if I understood it correctly).
I was wondering if there is a way to update the expires
property of a user. I would like to do the following:
expires
, such that the user does NOT expire anymoreAny suggestions will be highly appreciated!
Upvotes: 4
Views: 3936
Reputation: 13500
The only way to do that is to set the value to null
. You'd lose your createdAt
value, which you might not want so what I'd personally do is this:
expires
index on the existing field.expires
or deletionDate
with the 4 hour time to live.null
.I personally prefer to set the ttl
to 0 and set the date to {default: function() { return new Date(Date.now()+1000*60*number_of_hours); }
. That would make it easier to arbitrarily extend the time to live of a document and it's easier to read in the db.
Upvotes: 6