[英]How do I delete a component after some time on MongoDB?
I am currently creating a simple web app using Node.js and MongoDB. I am trying to delete whatever I enter into this field: shows what my webpage looks like我目前正在使用 Node.js 和 MongoDB 创建一个简单的 web 应用程序。我正在尝试删除我在该字段中输入的任何内容:显示我的网页的样子
When I enter something and click the button, it creates a collection in my MongoDB database.当我输入内容并单击按钮时,它会在我的 MongoDB 数据库中创建一个集合。 However, I want these collections to delete after some time.
但是,我希望这些 collections 在一段时间后删除。 I have tried using
db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
, but that did not work.我试过使用
db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
,但这没有用。 I have also tried doing:我也尝试过这样做:
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)
})
but that did not work either.但这也不起作用。 I am not sure what I have to do differently.
我不确定我必须做些什么不同的事情。 Sorry if this was somewhat confusing, I am new to Node and MongoDB!
对不起,如果这有点令人困惑,我是 Node 和 MongoDB 的新手! Thanks in advance!
提前致谢!
this works.这有效。 (I use Mongoose and MongoDB) I hope may it help.
(我使用 Mongoose 和 MongoDB)希望对您有所帮助。
createdAt: {
type: Date,
expires: '15m', // mention the time to expire
default: Date.now
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.