Shalini
Shalini

Reputation: 348

How to commit the content into Database using mem:node-replace?

I am having a code where i am using mem:node-replace(). The code is as below-

let $a := 
  for $i in $res
  let $uri := fn:base-uri($i)
  let $doc := fn:doc($uri)
  return if(fn:exists($doc) eq fn:true()) 
    then (
      (
        mem:node-replace($doc//*:NODE1,<NODE1>{doc($id)//*:NODE1}</NODE1>),
        mem:node-replace($doc//*:NODE2,<NODE2>{$curr_date}</NODE2>)
      ) 
    ) 
    else () 
return $a

I can able to see the value is getting replaced in the LOG but it is not reflecting into the database. How i am supposed to commit/save this node-replace in my database ?

Any Suggestions ?

Upvotes: 0

Views: 126

Answers (2)

If you need to update a node I would use xdmp:node-replace which replaces the node on disk.

Here's an example from the documentation: https://docs.marklogic.com/xdmp:node-replace

(: create an XML document :)
xdmp:document-insert("/example.xml",
    <a><b>bbb</b></a>);

(: replace the b node with a c node :)
xdmp:node-replace(doc("/example.xml")/a/b, <c>ccc</c>);

(: look at the new document :)
fn:doc("/example.xml")

Upvotes: 4

wst
wst

Reputation: 11771

The mem:replace function only updates the node in memory. You have created an in-memory copy of the document and made changes to it, but not propagated those changes in the database. This is similar to updating a variable passed by value instead of by pointer.

Following your in-memory updates, you need to insert the updated document back into the database.

let $a := for $i in $res
let $uri := fn:base-uri($i)
let $doc := fn:doc($uri)
let $_update := 
  if (fn:exists($doc) eq fn:true())
  then (mem:node-replace($doc//*:NODE1, <NODE1>{doc($id)//*:NODE1}</NODE1>), 
    mem:node-replace($doc//*:NODE2, <NODE2>{$curr_date}</NODE2>))    
  else ()
return xdmp:document-insert($uri, $doc)

Upvotes: 6

Related Questions