Skip to content

Instantly share code, notes, and snippets.

@aofleejay
Last active September 20, 2019 03:52
Show Gist options
  • Save aofleejay/c51fbd0ff4266183c3e26aca8150cc04 to your computer and use it in GitHub Desktop.
Save aofleejay/c51fbd0ff4266183c3e26aca8150cc04 to your computer and use it in GitHub Desktop.
Set mongodb document expiration from mongoose.
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/playground', {
useNewUrlParser: true,
})
const catSchema = mongoose.Schema(
{
name: String,
age: Number,
},
{ timestamps: true },
)
catSchema.index({ createdAt: 1 }, { expireAfterSeconds: 15 })
const Cat = mongoose.model('Cat', catSchema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment