Loading TOC...

op:write

op:write(
   [$docDescCols as map:map?]
) as map:map

Summary

Inserts or overwrites the documents identified by the uri column with the data supplied by the other document descriptor columns.

Parameters
$docDescCols A map to specify which document columns to use when writing. If this parameter is not passed in op:doc-cols() is used as default.

Usage Notes

op:write() needs to be run as an update transaction. Use 'declare option xdmp:update "true";' in the prolog, or use xdmp:invoke/xdmp:invoke-function with update="true".

See Also

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";

let $doc-descriptors := (
    map:entry("uri", '/optic/update/write4.xml')
      =>map:with("doc", <doc>write4</doc>),
    map:entry("uri", '/optic/update/write5.xml')
      =>map:with("doc", <doc>write5</doc>)
      =>map:with("collections", ("write")),
    map:entry("uri", '/optic/update/write6.xml')
      =>map:with("doc", <doc>write6</doc>)
      =>map:with("collections", ("write", "write6"))
      =>map:with("metadata", map:entry("operation","write6"))
  )
return op:from-doc-descriptors($doc-descriptors)
=>op:write(op:doc-cols())
=>op:result()
  

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";

let $doc-descriptors := (
    map:entry("uri", '/optic/update/write7.xml')
      =>map:with("doc", <doc>write7</doc>)
      =>map:with("collections", ("write", "write7"))
      =>map:with("metadata", map:entry("operation","write7")),
    map:entry("uri", '/optic/update/write8.xml')
      =>map:with("doc", <doc>write8</doc>)
      =>map:with("collections", ("write", "write8"))
      =>map:with("metadata", map:entry("operation","write8"))
      =>map:with("permissions", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update"))),
    map:entry("uri", '/optic/update/write9.xml')
      =>map:with("doc", <doc>write9</doc>)
      =>map:with("collections", ("write", "write9"))
      =>map:with("metadata", map:entry("operation","write9"))
      =>map:with("permissions", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update")))
      =>map:with("quality", 9)
  )
let $config := map:entry("uri","uri")=>map:with("doc","doc")=>map:with("collections","collections")
    =>map:with("metadata","metadata")=>map:with("permissions","permissions")=>map:with("quality","quality")
return op:from-doc-descriptors($doc-descriptors)
=>op:write($config)
=>op:result()
  

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";

let $rows := (
    map:entry("id",1)
      =>map:with("myQua",4)
      =>map:with("o_uri", '/test/fromDocUris/1.json')
      =>map:with("myUri", '/optic/update/write13.json')
      =>map:with("myColl", ("/optic/write", "newcol"))
      =>map:with("meta","dog")
      =>map:with("myPerm", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update"))),
    map:entry("id",2)
      =>map:with("myQua",5)
      =>map:with("o_uri", '/test/fromDocUris/2.json')
      =>map:with("myUri", '/optic/update/write14.json')
      =>map:with("myColl", "/optic/write")
      =>map:with("meta","cow")
      =>map:with("myPerm", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update"))),
    map:entry("id",3)
      =>map:with("myQua",6)
      =>map:with("o_uri", '/test/fromDocUris/3.json')
      =>map:with("myUri", '/optic/update/write15.json')
      =>map:with("myColl", "/optic/write")
      =>map:with("meta","tiger")
      =>map:with("myPerm", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update")))
  )
let $row-col-types := (
  map:entry("column", "id") => map:with("type", "integer")=> map:with("nullable", false()),
  map:entry("column", "myQua") => map:with("type", "integer")=> map:with("nullable", true()),
  map:entry("column", "o_uri") => map:with("type", "string")=> map:with("nullable", false()),
  map:entry("column", "myUri") => map:with("type", "string")=> map:with("nullable", false()),
  map:entry("column", "myColl") => map:with("type", "none")=> map:with("nullable", true()),
  map:entry("column", "meta") => map:with("type", "string")=> map:with("nullable", true()),
  map:entry("column", "myPerm") => map:with("type", "none")=> map:with("nullable", true())
)
let $config := map:entry("uri","myUri")=>map:with("doc","myDoc")=>map:with("collections","myColl")
    =>map:with("metadata","myMeta")=>map:with("permissions","myPerm")=>map:with("quality","myQua")
return op:from-param("bindingParam", "", $row-col-types)
  =>op:join-doc(op:col("myDoc"), op:col("o_uri"))
  =>op:bind(op:as("myMeta", op:json-object((op:prop("meta", op:json-string(op:col("meta")))))))
  =>op:order-by("id")
  =>op:write($config)
  =>op:result("object",map:entry("bindingParam",$rows))
  

Example

(: Write a temporal document:)
xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";

let $doc-descriptors := (
    map:entry("uri", '/optic/update/temporal/temporal1.json')
      =>map:with("doc", xdmp:unquote('{"desc":"temporal doc 1"}',(),"format-json"))
      =>map:with("collections", ("temporal", "write"))
      =>map:with("metadata", map:entry("validStart","2014-04-03T11:00:00")=>map:with("validEnd","9999-12-31T11:59:59Z"))
      =>map:with("permissions", (xdmp:permission("rest-reader","read"),xdmp:permission("rest-writer","update")))
      =>map:with("quality", 1)
      =>map:with("temporalCollection","update")
)
return op:from-doc-descriptors($doc-descriptors)
  =>op:write()
  =>op:result()
  

Example

(: Overwrite document collections :)
xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
declare option xdmp:update "true";

let $doc-descriptors := (
    map:entry("uri", '/optic/update/write1.xml')
      =>map:with("collections", ("overwrite","overwrite10")),
    map:entry("uri", '/optic/update/write2.xml'),
    map:entry("uri", '/optic/update/write3.xml')
      =>map:with("collections", ("overwrite","overwrite12"))
)
return op:from-doc-descriptors($doc-descriptors)
  =>op:order-by("uri")
  =>op:write(op:doc-cols((), ("uri","doc","collections")))
  =>op:result()
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.