Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid practice to set an 'expire_at' field to null to cancel expiration in MongoDB?

I have a schema where I expire the document in 24 hours.

let mySchema = new Schema({
  name: String,
  createdAt: {type: Date, default: Date.now },
  expire_at: {type: Date, default: Date.now, expires: 86400},
});

However, on some occasions I do not want to expire the document and then do myDocument.expire_at = null;

This seems to work and the document seems to not expire. However, are there better practices for achieving this or any problems that might occur if the document expiry is cancelled in this way?

like image 541
danefondo Avatar asked Oct 20 '25 05:10

danefondo


1 Answers

Setting a field that has an ttl index on it to NULL to not have it expire is documented and thus a valid way to do it. You could also remove the entire field rather than setting it to NULL. (I'd prefer to keep the value; that way it's not ambiguous if the value is missing on purpose or not.)

like image 168
RickN Avatar answered Oct 21 '25 19:10

RickN