Mongoose TimeSeries Collection data is not deleted even after adding expireAfterSeconds

41 Views Asked by At

I have created a mongodb timeseries collection the problem is that the documents are not deleted after the expiry time and in mongodb atlas i can see the ttl index.

Package Verison: "mongodb": "^5.5.0", "mongoose": "^6.5.1",

The Schema is provided below:

const mongoose = require("mongoose");

const cronErrorSchema = new mongoose.Schema(
  {
    title: { type: String }, // error title
    goal_support_id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "GoalSupport",
    },
    user_id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },
    message: { type: String }, // error message
    timestamp: { type: Date, default: Date.now }, // error timestamp
    metadata: { type: Object }, // error metadata
  },
  {
    timeseries: {
      timeField: "timestamp", // field name for timestamp
      metaField: "metadata", // field name for metadata document
      granularity: "hours", // Default is "seconds" but we have to change it to "hours" as we do not have data for every second
    },
    autoCreate: false, // disable `autoCreate` since `timestamps` is enabled
    expireAfterSeconds: 120, // 120 sec
  }
);

module.exports = mongoose.model("CronError", cronErrorSchema);

Kindly let me know what i am doing wrong here.

I have tried multiple solutions and check mongoose documentation but it doesn't work.

0

There are 0 best solutions below