在特定时间删除MongoDB文档 [英] Delete MongoDB document at specific time

查看:52
本文介绍了在特定时间删除MongoDB文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在特定时间删除文档.

I am looking into deleting a document at a specific time.

const TestSchema = new Schema({
 expire_at: {
 type: Date,
 },
}, {
 timestamps: true,
});
TestSchema.index({expire_at: 1}, {expireAfterSeconds: 0});

开机自检

const test = new TestSchema(this.request.body);
  test.expire_at = test.end_time;

  try {
    yield test.save();
  } catch (error) {
    this.status = 409;
    this.response.body = error.errors;
    return;
  }

  this.response.body = test;
  this.status = 201;

似乎文件没有在expire_at中指定的时间删除.

It does not seem that the documents delete at the time specified in expire_at.

我正在使用以下日期格式:2016-07-20T05:01:19.567Z

I am using this Date format: 2016-07-20T05:01:19.567Z

推荐答案

这将在两个小时内删除文档:

This will delete the document in two hours:

const TestSchema = new Schema({
    expire_at: {type: Date, default: Date.now, expires: 7200} 
})

//expired in 2 hours

这篇关于在特定时间删除MongoDB文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆