1

I am trying to perform 2 operations in one findOneAndUpdate():

  • Update date in one field lastUpdatedTimestamp, set it to current date (this one works fine in my statement),
  • Update date in other field expiryTimestamp, by adding 1 day to $currentDate (I couldn't find a way to achieve it so I'm trying to $add 1 day to the the value read from the above field lastUpdatedTimestamp) - (I can't make this one work).

    findOneAndUpdate(
        {"_id":123}, 
        { $currentDate: {"lastUpdatedTimestamp":true}, $set: {"expiryTimestamp": {$add: ["$lastUpdatedTimestamp", 24*60*60000]}}}
    )
    

Here's the error I'm receiving: { "ok" : 0.0, "errmsg" : "The dollar ($) prefixed field '$add' in 'expiryTimestamp.$add' is not valid for storage.", "code" : 52 }

Is it even possible? I'd appreciate your help.

styvane
  • 59,869
  • 19
  • 150
  • 156
Jacek
  • 194
  • 1
  • 11
  • @user3100115 thanks for your suggestion. My exact problem lies in the `$add` expression. Is it permitted in that context, or only in `aggregate()`? – Jacek Jun 05 '16 at 07:10
  • 2
    You can only use `$add` with `aggregate()`. Also do you want to use the `$currentDate` to set "expiryTimestamp"? – styvane Jun 05 '16 at 07:14
  • I want to use `$currentdate + 1 day` for `expiryTimestamp` – Jacek Jun 05 '16 at 07:16
  • So why are you using "lastUpdatedTimestamp" to set "expiryTimestamp"? – styvane Jun 05 '16 at 07:18
  • I have amended the question. What I'm trying to achieve is to add time to `$currentdate`. Something like `dateAdd(day, 1, getDate())` in SQL. Is it possible? – Jacek Jun 05 '16 at 07:46
  • To update one field using another field in the same document, you can refer [this answer](http://stackoverflow.com/questions/3788256/mongodb-updating-documents-using-data-from-the-same-document/3792958#3792958) – gzc Jun 05 '16 at 07:47
  • @gzc the answer you linked has nothing to do with this question – styvane Jun 05 '16 at 08:07
  • @user3100115 OP want to update `expiryTimestamp` by adding 1 day to new `lastUpdatedTimestamp`. – gzc Jun 05 '16 at 08:26
  • @gzc note that what OP really wants to do is update "expiryTimestamp" to `$currentDate + 1` – styvane Jun 05 '16 at 08:42

2 Answers2

0

You can use the setDate() method to set the "expiryTimestamp" value.

db.collection.updateOne(
    { "_id": 123 },  
    { "$set": { 
        "lastUpdatedTimestamp": new Date(),  
        "expiryTimestamp": new Date().setDate(new Date().getDate() + 1) 
    }}
)

You don't need to use findOneAndUpdate unless you want to return the new or old document.

styvane
  • 59,869
  • 19
  • 150
  • 156
0

The marked answer is wrong in the sense that using new Date() will not use database timestamp which is important if your server and database hosted on different region and also count network time for sending data. To correctly do this, use $currentDate like this:

db.collection.findOneAndUpdate({_id: 123 }, {
   $set: { /** update doc here **/ },
   $currentDate: { lastUpdatedTimestamp: true} 
});

Similarly using updateOne

db.collection.updateOne({_id: 123 }, {
   $set: { /** update doc here **/ },
   $currentDate: { lastUpdatedTimestamp: true} 
});
Ashish Rawat
  • 3,363
  • 5
  • 29
  • 35