[SERVER-40266] Add on modified option to $currentDate Created: 21/Mar/19  Updated: 25/Mar/19  Resolved: 22/Mar/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Bryan Lee Assignee: Danny Hatcher (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-6566 Support conditional updates: $updates Closed
Participants:

 Description   

$currentDate will set the current date regardless of whether the document was otherwise modified.

It would be helpful to have an option to set the current time only if fields other than those specified in $currentDate are changed from their original values as returned in the nModified count, similar to MySQL's ON UPDATE CURRENT_TIMESTAMP.

Using change streams or tailing oplog could accomplish such, but requires replication.



 Comments   
Comment by Bryan Lee [ 25/Mar/19 ]

Ah yes, SERVER-14094 would accomplish this nicely, thank you!

Comment by Asya Kamsky [ 23/Mar/19 ]

bryanleepartstree I think either SERVER-6566 or SERVER-14094 would provide the functionality you’re looking for when implemented, yes? It seems conditional updates would be useful for all modifiers, not just specifically $currentDate.

Comment by Bryan Lee [ 22/Mar/19 ]

Thanks for the feedback, but I'd like to differentiate between "updated" and "modified".  Ticket SERVER-13578 only describes $setOnUpdate being triggered on update vs insert, and would trigger on update requests regardless of whether or not the document has been modified from its original values.

The write result of an update request includes nModified, which is only greater than one if a value in the document has changed from its original value, which is why nMatched might be greater than nModified.  My request is to examine whether or not it's possible to leverage the knowledge of document state change in determining whether or not the $currentDate op should be fulfilled.

 

Example:

If we have a collection with the following document

{id : 1, name : "one", lastModified : "2000-01-01T00:00:00+00:00"}

 

And issue an update

update({id : 1}, {$set : {name : "one"}})

The response would return nModified : 0, and the document would remain the same

 

However if I issue an update with $currentDate

update(
  {id : 1},
  {
    $set : {name : "one"},
    $currentDate : {lastModified : true}
  }
)

The response would return nModified : 1, and lastModified would contain the date of the writeOp.  I am recommending an option to only fulfill the $currentDate op if nModified would otherwise return >0

 

Something like

update(
  {id : 1},
  {
    $set : {name : "one"},
    $currentDate : {lastModified : "modify"}
  }
)
//returns nModified 0 and the request results in noop 

But

update(
  {id : 1},
  {
    $set : {name : "one wow so much different"},
    $currentDate : {lastModified : "modify"}
  }
)
//returns nModified 1 and $currentDate successfully updates lastModified

 

 

Comment by Danny Hatcher (Inactive) [ 22/Mar/19 ]

Hello Bryan,

Thanks for your feature request! There is an existing ticket open as SERVER-13578 to enable $setOnUpdate. This should cover your use case as you would be able to use $currentDate only when the document was actually updated.

As such, I will close this ticket as a duplicate.

Thanks,

Danny

Generated at Thu Feb 08 04:54:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.