1
const notificationSchema = mongoose.Schema({
    type:{
        type: String
    },

    message:{
       type: String
    },
    userId:{
        type: String,
        required: true,

    },
    timestamp:{
        type: Date,
        default: new Date()

    },
    expireAt: {
        type: Date,
        default: Date.now,
        index: { expires: '5m' },
      },
})

我的数据没有在猫鼬中自动删除,我的模型有问题吗?这是我的模型结构。有人可以帮忙吗

4

2 回答 2

1
const notificationSchema = mongoose.Schema({
type:{
    type: String
},

message:{
   type: String
},
userId:{
    type: String,
    required: true,
},
{ 
  timestamps: true
}
});

notificationSchema.index({createdAt: 1},{expireAfterSeconds: 3600});

集合中的每个字段将在 3600 秒后被删除

于 2020-03-28T12:16:10.473 回答
0

有几种方法,但想到的一种方法是 TTL。

“TTL 索引是特殊的单字段索引,MongoDB 可以使用它在一定时间或特定时钟时间后自动从集合中删除文档。”

在此处了解更多信息 > https://docs.mongodb.com/manual/core/index-ttl/

对于猫鼬> https://github.com/mongoosejs/mongoose-ttl

于 2020-03-28T16:33:47.080 回答