Add in current timestamp while upsert

I want to upsert a document with timestamp value of now + 2 seconds

Right now, I am doing

db.machine_status.update({“private_ip”: “'”${private_ip}“'”}, {“private_ip”: “10.0.0.4”, “public_ip”: “1.2.3.4”, “state”: “IDLE”, “updated_time”: new Date()}, {“upsert”: true})

But this is inserting current timestamp

I want current timestamp + 2 seconds, how do I do it ?

Hello, @Rahul_Bansal! Welcome to the community!

You need to calculate your new date at your application level:

var dateToInsert = new Date(
  Date.now() + 2 * 1000
);

db.test1.updateOne(
  {
    privateIp: '<ip>',
  },
  {
    $set: {
      updateTime: dateToInsert,
    }
  },
  {
    upsert: true,
  }
);
2 Likes

Oh, okay… Got it.

Thanks for the welcome @slava
Hope you’re doing well

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.