Search code examples
orientdb

orientdb only update field if null


I am trying to find a way to run an update query with orientdb where I update multiple properties. I will have two properties on an edge, one called acknowledged_at and one called read_at. The acknowledged_at field can get updated prior to the read_at getting updated. However I want to make sure that when I update all of the read_at fields, that I also ensure that any acknowledged_at fields get updated with the current timestamp as well (if they weren't previously set).

My use case for this is having facebook like notifications. A user can acknowledge a notification but not read it at that time. But if they click a read_all button, or decide to "read" just that notification later, then I want to make sure I am also updating the acknowledged_at property (if it is null).

Is there any way to do a case statement type update on the acknowledged_at field to only update if null?

UPDATE: Just to make it more clear, I always want to update the read_at field but only want to update the acknowledged_at field if it is null. If it was previously acknowledged I want that value to remain.

UPDATE EDGE received SET read_at = '#{DateTime.now}', acknowledged_at = '#{DateTime.now}' WHERE out.employeeId = '#{employeeId}'

Solution

  • UPDATE EDGE received SET ... WHERE acknowledged_at IS NULL

    UDPATE

    this works for me

    update edge received set read_at = "2017-06-23 19:29:00",acknowledged_at=ifnull(acknowledged_at, "2017-06-23 19:29:00") WHERE out.employeeId = 'A002'
    

    ref. https://orientdb.com/docs/2.2/SQL-Functions.html