How to delete object after 24hrs based on the createdAt field?

I’m trying to buld a model in which users will publish their own objects, however I want these to be deleted after 24rs have passed via a cron-job. I currently use mongoose to make my queries to my MongoDB.

I’m assuming the query will be taking consideration of the createdAt field. Is there a way to make this possible?

const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const PostSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
      default: 'No title',
    },
    slug: {
      type: String,
      required: true,
      default: 'no-title',
    },
    text: {
      type: String,
      required: true,
      default: 'No text',
    },
    status: {
      type: String,
      required: [true, 'Please add a status option'],
      enum: ['draft', 'published', 'trash'],
      default: 'published',
    },
    postType: {
      type: String,
      required: true,
      enum: ['post', 'story'],
      default: 'post',
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
    timestamps: true,
    id: false,
  }
)

I found out that the best approach would be by adding an addedOn/createdAt field with expire on it which I believe would work great but what if the objects that I need to delete are those under the ‘story’ postType field only?

I believe another post here says mongodb ttl index only supports root level document. (it can only delete all but not a part of the doc).

in the latter case you will have to implement your own logic by running a cron , for instance