Applying a function in the context of a different database - MarkLogic 7 update 01 May 2014

A couple of years ago I wrote about how useful it would be to apply a function in the context of a different database. At that time there were two choices - eval a string or invoke a main module. But things have changed since then, in MarkLogic 7 there are a couple of new functions:

xdmp:invoke-function(
   function() { xdmp:document-insert("doc", $content), xdmp:commit() },
   <options xmlns="xdmp:eval"><database>{xdmp:database("another-db")}</options>
)
xdmp:spawn-function(
   $fn,
   <options xmlns="xdmp:eval"><database>{xdmp:database("another-db")}</options>
)

The documentation explains the details better than I could: xdmp:invoke-function, xdmp:spawn-function. And check out taskbot for a great example of these new functions in action.