Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About

I’m using ML8 and Node.js. The documentation here: http://docs.marklogic.com/guide/node-dev/documents#id_68765 describes how to do conditional updates in ML using the versionId field.

But for example if I want to do a conditional update on a different field, is it possible?

My scenario is: I have JSON documents with elements assignedTo and assignDate (where assignDate is set to current date every time a new value is set to assignedTo)

Now, for my “Assign” operation, I would like to make sure that no one else has changed the assignedTo/assignDate fields between the time I read the document and when I perform the update. I don’t care if other fields in the same document have been updated or not - if other fields have been updated, I can still proceed with the Assign operation (hence I cannot use the versionId approach, since that covers the whole document)

How can this be done?

Comments

Optimistic locking doesn’t look at the content of the document. Instead, the version id is a system value reflecting the timestamp on the document. Checking the timestamp is more efficient than reading the document content.

You could write a POST resource service extension that

  • receives the update content
  • reads the document and checks that assignTo/ assignDate haven’t changed
  • does an xdmp:node-replace() on the root of the document with the update content

Hoping that helps,