简体   繁体   中英

How can I update the expires property on createdAt field in mongoose to avoid the document being deleted?

I am trying to add account verification to this app and I have set a user account that has been created but not verified to delete itself after 30 seconds using.

createdAt: {
  type: Date,
  default: Date.now,
  index: {
    expires: 30,
  },
},

But now I am looking for a way to prevent the document from being deleted after 30 seconds but I haven't found a working solution.

This should do the trick for you:

Let's say this is User model.

await User.syncIndexes();

The other thing you will need to update in User model is:

createdAt: {
  type: Date,
  default: Date.now,
},

Mongoose uses MongoDB TTL indexes to expire documents, see https://docs.mongodb.com/manual/core/index-ttl/

If the indexed field in a document is not a date or an array that holds a date value(s), the document will not expire.

If a document does not contain the indexed field, the document will not expire.

So to prevent the document expiring you might unset the field, set it to a non-date value, or set the date into the far future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM