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

Viewed 328

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?

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.)

Related