如何在MongoDB上一段时间后删除组件?



我目前正在使用Node.js和MongoDB创建一个简单的Web应用程序。我正在尝试删除我在此字段中输入的任何内容: 显示我的网页的外观

当我输入某些内容并单击按钮时,它会在我的MongoDB数据库中创建一个集合。但是,我希望这些集合在一段时间后删除。我试过使用db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } ),但没有用。我也尝试过做:

app.get('/', (req, res) =>
{
**db.collection('test').find().toArray().createIndex( { "createdAt": 1 }, { expireAfterSeconds: 60 } )**
.then(test => {
res.render('index.ejs', {test: test})
//console.log(results)
})

.catch(error => console.error(error))
// res.render('index.ejs', {}); 
//res.render(view, locals)
})

但这也没有用。我不确定我必须做些什么不同的事情。抱歉,如果这有点令人困惑,我是 Node 和 MongoDB 的新手!提前感谢!

这行得通。(我使用猫鼬和MongoDB(我希望它能有所帮助。

createdAt: { 
type: Date, 
expires: '15m',  // mention the time to expire
default: Date.now 
}

最新更新