【问题标题】:MongoDb automatically update data after a specified number of timeMongoDb 在指定的时间后自动更新数据
【发布时间】:2021-08-17 11:31:33
【问题描述】:

我有一个类似的 MongoDb 对象

{
  "_id": 1.
  "updateAfter":"2021-06-28T03:31:53.000Z",
  "isPassed": false
}

我想在指定时间"updateAfter"之后更新"isPassed": true

假设当前时间是 5 月 29 日下午 12:30。 updateAfter : 30 May 10: 50 am。当当前时间超过 5 月 30 日(或大于当前时间)时,我正在尝试 isPassed: true

这是否可以通过 MongoDB TTL indexing 进程或任何其他想法(如 time scheduler 进程)实现?

我该怎么做?

提前致谢

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query mongoose-schema


    【解决方案1】:

    你可以使用聚合update()

     db.collection.update({},
    [
      {
        $set: {
          isPassed: {
            $gte: [
              "$updateAfter",
              {
                $toString: new Date()
              }
            ]
          }
        }
      }
    ])
    

    工作Mongodb

    【讨论】:

    • 这里,你的意思是聚合作为聚合框架还是不是聚合框架?
    • 我可以在不更新的情况下使用 myModel.findOneAndUpdate({}) 吗?
    • 在您的查询中我在哪里使用“isPassed: true”?
    • 是的。但是您使用“2021-07-28T03:31:53.000Z”是当前时间吗?我的查询必须检查当前时间(运行时间或机器时间)是否大于“updateAfter”时间。
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多