I have the following schema in my App:
const storySchema = new mongoose.Schema({
createdAt: { type: Date, expires: 10000 , default: Date.now },
title: {type: String},
creator: {type: Mongoose.types.ObjectId, ref: "User"},
tags: [{type: String}],
....
})
currentSchema.index({"createdAt": 1 },{ expireAfterSeconds: 10000 });
The code above adds an index
to ensure every story
document is removed after 10000
ms. This code works correctly.
However I have a user schema designed like so:
const userSchema = new mongoose.Schema({
stories: [{ type: Mongoose.Schema.Types.ObjectId, ref: "Story" }],
title: {type: String},
bio: {type: String},
....
})
When a story is automatically removed/expired
I would like to automatically remove that story from the stories
array of objectids
which is stored on the creators document. I tried adding a pre('remove')
middleware on the User model, however this isn’t fired when a document expires.
Thanks