k88074
k88074

Reputation: 2164

Updating "expires" with mongoose

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:

Any suggestions will be highly appreciated!

Upvotes: 4

Views: 3936

Answers (1)

RickN
RickN

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:

  1. Remove the expires index on the existing field.
  2. Create a field named expires or deletionDate with the 4 hour time to live.
  3. If the user should not be deleted, set the value to 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

Related Questions