Instagram clone | How to handle stories being visible only for 24h?

Title says it all. How would you handle Instagram stories being visible only 24h after being posted? I'm working with mongooose. I found there is an expires option available in second parameter of mongoose.Schema():
import mongoose from 'mongoose'

const oneDay = 86400 // seconds

const storySchema = new mongoose.Schema({
author: { type: mongoose.SchemaTypes.ObjectId, ref: 'User' },
content: { type: [String], required: true }
},
{
timestamps: true,
expires: oneDay // 👈
}
)

const Story = mongoose.models.Story || mongoose.model('Story', storySchema)

export default Story
import mongoose from 'mongoose'

const oneDay = 86400 // seconds

const storySchema = new mongoose.Schema({
author: { type: mongoose.SchemaTypes.ObjectId, ref: 'User' },
content: { type: [String], required: true }
},
{
timestamps: true,
expires: oneDay // 👈
}
)

const Story = mongoose.models.Story || mongoose.model('Story', storySchema)

export default Story
But I'm not sure what happens after it expires? I don't want it to get deleted. I still want to have the story in my DB (so user can add it to highlights, view it in his archive etc.), just not visible to users anymore. I was also thinking of just using createdAt which is coming from timestamps: true and then somehow filter stories that are posted within 24h and display them to the user. Not really sure if that's the best way to go. Any ideas/solutions/recommendations?
3 Replies
Jochem
Jochem•13mo ago
The easiest way is definitely filtering on createdAt or a separate publishedAt column At Instagram scale it might be something different, but you really, really don't need to worry about that, filtering on those columns will work up to millions of rows
ErickO
ErickO•13mo ago
imma keep it real, they prolly aren't using mongo at instagram and yeah I'd just go for a timestamp and display stories within a certain timeframe as far as the "expires" field, I'd assume that's mongoose setting a TTL (time-to-live) index which deletes your documents after the specified time, so yes, it would get deleted
Tenkes
TenkesOP•13mo ago
Alright, thank you both for useful info!
Want results from more Discord servers?
Add your server