1

I have an MongoDb Object like

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

I want to update the "isPassed": true after the specified time "updateAfter".

Lets say the current time is 29 May 12:30 pm. The updateAfter : 30 May 10: 50 am. I am trying to isPassed: true when the current time is over the 30 May(Or greater than the current time).

Is this possible with the MongoDB TTL indexing process or any another idea like time scheduler process?

How can i do this?

Thanks in advance

Mijanur Rahman
  • 1,094
  • 1
  • 11
  • 20

1 Answers1

-1

You can use aggregation update()

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

Working Mongodb

varman
  • 8,704
  • 5
  • 19
  • 53
  • Here, Did you mean aggregation as a aggregation framework or not? – Mijanur Rahman May 29 '21 at 14:09
  • Yes, you can refer https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/ – varman May 29 '21 at 14:10
  • Can i use myModel.findOneAndUpdate({}) without only update? – Mijanur Rahman May 29 '21 at 14:12
  • Where i use "isPassed: true" in your query? – Mijanur Rahman May 29 '21 at 14:35
  • Yes. But you use "2021-07-28T03:31:53.000Z" is it the current time? My query have to check the current time(the running time or machine time) is greater than the "updateAfter" time. – Mijanur Rahman May 29 '21 at 16:20
  • So you can easily pass `new Date()`, no?. I dont know about mongoose – varman May 29 '21 at 16:24
  • Basically new Date is a current date. But i am trying to say, i am checking the time between "updateAfter" and the current machine time. You checking between two fixed value.But i want to query with time limit value.Basically when the current time will be greater than the "updateAfter", then "isPassed: true". – Mijanur Rahman May 29 '21 at 16:59
  • Because after starting the server or the mongoDb queary, the variable new Date() won't update. So, How can i check that the current time is greater than the "updateAfter" time? – Mijanur Rahman May 29 '21 at 17:13
  • No, what I try to say if You can pass the current datetime into `$gte: [ "$updateAfter", // set the date time here ]` sometimes this might help to get the curret date [https://stackoverflow.com/questions/7675549/how-do-i-update-a-property-with-the-current-date-in-a-mongoose-schema-on-every-s/9056645] – varman May 29 '21 at 17:30
  • You need to pass the current date time into the query – varman May 29 '21 at 17:31
  • When i pushing new Date(). It dose not update after. So how do i check the current time is greater than the "updateAfter" time. Can i use seTimeOut or setInterval types functionality in MongoDb? – Mijanur Rahman May 29 '21 at 17:45
  • I think that i can not properly explain the problem with you.Lets say the current time is 29 May 12:30 pm. The "updateAfter : 30 May 10: 50 am". This is always true cause "updateAfter" is always greater when we push the data into database. But i am trying to "isPassed: true" when the current time is over the 30 May(Or greater than the current time). – Mijanur Rahman May 29 '21 at 18:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233070/discussion-between-varman-and-mijanur-rahman). – varman May 30 '21 at 04:10