猫鼬期满

时间:2017-12-18 02:40:20

标签: node.js mongodb mongoose mongoose-schema

我有以下架构:

ip: String,
port: Number,
msgboard: [{
   date: {
        type: Date,
        default: Date.now,
        expires: 120
        },
   msg: String
}]

我希望在创建120秒后自动删除邮件。但上面的删除是整个文档而不仅仅是msgboard中的子文件。 我一直在使用cron并运行一个函数,但代码似乎太乱了。有没有内置的方式?

1 个答案:

答案 0 :(得分:2)

I think you should try this, its working. I have created two schemas for this problem

msgboard schema :

for

Main Test Schema : In which reference of msgboard is stored

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var msgboardSchema = new Schema({
    date: {
        type: Date,
        default: Date.now,
        expires: 120
        },
    msg: String
});

module.exports = mongoose.model('msgboard', msgboardSchema);

As msgboard is separate from Test, this will only remove msgboard subdocumet from Test after 120s and not whole test document.