[英]Deleting Documents 1 Month later after their creation in MongoDb with NodeJS
I have a log collection and would like to remove logs which have been created 1 month ago. 我有一个日志收集,想删除1个月前创建的日志。
I am using Mongoose in NodeJS and I've tested with "minute" parameter and it worked. 我在NodeJS中使用Mongoose,并且已经使用“ minute”参数进行了测试,并且可以正常工作。 But the problem that I want to be sure that the documents will be deleted one month later. 但是我想确定一个月后将文件删除的问题。
Here what I did 这是我做的
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.set('useCreateIndex', true);
mongoose.connect('mongodb://localhost:27017/blabla', {useNewUrlParser: true});
mongoose.Promise = global.Promise;
var Logs = new Schema({
AppName:{
type: String,
required: [true,'Application Name field is required']
},
CreatedDate:{
type: Date,
required: [true,'Created Date field is required'],
},
ExpireAt: { type: Date, default: Date.now, expires: '2m' },
});
module.exports.Logs = mongoose.model('logs', Logs);
as you can see, "expires" paramter has 2m (2 minutes) My concern that If I give "1M" (one month), would it accept it? 如您所见,“ expires”参数为2m(2分钟)我担心如果我给“ 1M”(一个月),它会接受吗? I have not found acceptable paramters. 我还没有找到可接受的参数。
http://www.albertgao.xyz/2019/02/07/how-to-auto-delete-mongodb-records-after-certain-time-with-mongoose/ http://www.albertgao.xyz/2019/02/07/how-to-auto-delete-mongodb-records-after-certain-time-with-mongoose/
I've tried to give direct date + 1month or numeric value as seconds but did not work. 我试图给直接日期+ 1个月或数值作为秒,但没有用。
So how can I let delete documents one month later after their creations.. 因此,如何让文档在创建后一个月后删除。
You can do it with 1m
like as following snippet , 您可以像下面的代码片段一样在1m
距离内完成操作,
expireAt: {
type: Date,
default: Date.now,
index: { expires: '5m' },
},
5m is a shortcut here, you can refer to this package to check how to use words rather than number to make the setup more readable. 5m是这里的快捷方式,您可以参考此软件包来检查如何使用文字而不是数字来使设置更易读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.