从for循环插入文档时获取XDMP-CONFLICTINGUPDATES

时间:2018-10-30 15:21:49

标签: json marklogic marklogic-8

我建立了一个REST端点,该端点从单个JSON文件接收记录,并在解析后插入/更新每个项目。

{
  "records" : [
  {
    "description" : "test",
    "createDate" : "20181018 125816.220",      
    "owner" : "Smith, Bob",
    "uniqueID" : "REF-12-3456",      
    "status" : "Draft",
    "title" : "test record for Bob"
  },
  {
    "description" : "test 3",
    "createDate" : "20181018 125816.220",      
    "owner" : "Jones, Ben",
    "uniqueID" : "REF-12-7890",      
    "status" : "Draft",
    "title" : "test record for Ben"
  },
  {
    "description" : "test",
    "createDate" : "20181018 125816.220",      
    "owner" : "Smith, Bob",
    "uniqueID" : "REF-12-3456",      
    "status" : "Complete",
    "title" : "Completed test record for Bob"
  },
 ]
}

我的端点将JSON发送到一个函数,该函数解析每个项目,并从uniqueID字段创建一个URI,将每个节点发送到一个函数,以添加元数据节点并插入文档。

declare function local:doc-from-web($doc as document-node()*)
{
  let $message :=
    for $item in $doc/managementOfChange
     let $uri := $item/uniqueID
     let $loaded :=  local:load-doc($kms,$uri)
     return $loaded  
  return $message
};

declare function local:load-doc(
  $doc as object-node(),
  $uri as xs:string
)
{
  let $metadata := json:object()
  let $_ := map:put($metadata, "commonDate", $doc/createDate)
  let $_ := map:put($metadata, "commonStatus", $doc/status[1])        
  let $_ := map:put($metadata, "ingestDate", fn:current-dateTime())

  let $metadataJson := xdmp:to-json($metadata)

  let $object := json:object()
  let $_ :=  map:put($object,"RECORD",$doc)
  let $_ :=  map:put($object,"metadata", $metadataJson)

  let $newDoc := xdmp:to-json($object) 

  let $insert := xdmp:document-insert(
    $uri,
    $newDoc,
      ( xdmp:permission("rest-reader", "read"),
        xdmp:permission("rest-writer", "update") ),
      ("collection1","collection2")
  )

return “Document Inserted”
};

因为JSON加载文件中可能存在同一记录的更新版本(请参见我的示例数据中的节点1和3),并且for循环在单个事务模式下运行,因此导致更新冲突。

有办法避免这种情况吗?有没有更好的方法来处理单个文件中的数据?

谢谢。

0 个答案:

没有答案