Hi,
I’m trying to develop a locking mechanism that uses MongoDB’s date/time to know whether a lock has expired or not, and if so, get the lock. I’m using findOneAndUpdate()
like this:
findOneAndUpdate(
{"_id": "my-lock","$or" :[{"state": {"$ne": "locked"}},{"lockedUntil": {"$lt": new Date()}}]},
[ { $set: { "lastAcquiredAt": "$$NOW", "state": "locked", "lockedUntil" : { $add: ["$$NOW", 15000 ] } } } ],
{ upsert:true, returnNewDocument : true }
);
In the $set
, I’m using $$NOW
to get the current date/time, which works fine. However I can’t use that in the filter, where I’m checking if the current lock has expired and I can acquire it. How do I go about using MongoDB date/time in a filter? I can use new Date()
to return it, but is that the best/only way? I’ve also tried adding a $match
pipeline, but it’s not allowed using findOneAndUpdate()
.