Skip to content

Instantly share code, notes, and snippets.

@Igor-Lopes
Created February 2, 2018 16:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Igor-Lopes/fbb3d866a9331bba1e561936757ab18b to your computer and use it in GitHub Desktop.
Save Igor-Lopes/fbb3d866a9331bba1e561936757ab18b to your computer and use it in GitHub Desktop.
Add timestamps and expiration to mongoose schema
var schema = mongoose.Schema({
email: {
type: String,
required: true,
lowercase: true,
index: {
unique: true
}
},
expireAt: {
type: Date,
/* Defaults 7 days from now */
default: new Date(new Date().valueOf() + 604800000),
/* Remove doc 60 seconds after specified date */
expires: 60
}
/* Automatically add createdAt and updatedAt fields to schema */
}, { timestamps: true })
@Mr-M1M3
Copy link

Mr-M1M3 commented Feb 6, 2022

Could you please tell me what did you mean by "specified date" more specifically?

@boly38
Copy link

boly38 commented Mar 23, 2022

I tried to detail the "expires" option in the following gist: https://gist.github.com/boly38/2d3ead05b2491924ec97ac4d18a86528

@DjibrilM
Copy link

DjibrilM commented Sep 5, 2022

i would like to set the expiration time when the schema get created no a default one !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment