Along with traditional updates (i.e. replacing an entire document), MarkLogic supports atomic, in-place updates, allowing you to update existing values for a document. Currently, MarkMapper is not taking advantage of this feature and is still doing full document updates.
Let’s start with a simple Page model:
class Page
include MarkMapper::Document
key :title, String
key :day_count, Integer, :default => 0
key :week_count, Integer, :default => 0
key :month_count, Integer, :default => 0
key :tags, Array
end
The atomic modifier operations can be performed directly on instances of your MarkMapper class, or on a collection by passing in the ID or criteria of the documents you wish to modify.
Set the values for the keys.
@page.set(:title => "New Home")
Page.set({:title => 'Home'}, :title => "New Home")
Page.set(@page.id, @page2.id, :title => "New Home")
Unset or remove the given keys.
@page.unset(:title)
Page.unset({:title => 'Home'}, :title)
Page.unset(@page.id, @page2.id, :title)
Append one value to the array key.
@page.push(:tags => 'foo')
Page.push({:title => 'Home'}, :tags => 'foo')
Page.push(@page.id, @page2.id, :tags => 'foo')
Append several values to the array key.
@page.push_all(:tags => ['foo','bar'])
Page.push_all({:title => 'Home'}, :tags => ['foo','bar'])
Page.push_all(@page.id, @page2.id, :tags => ['foo','bar'])
When applying a modifier operation on a variable (local or instance), make sure to reload the variable. MarkMapper does not update the state of the variable unless you explicitly tell it to like so:
@page.set(:title => "Something New")
@page.title # => "Something Old"
@page.reload
@page.title # => "Something New"