You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MarkLogic跨数据库复制文档时xdmp:document-insert执行失败

Fixing "arg2 is not of type node()" Error in MarkLogic Document Copy XQuery

Let's break down what's going wrong and fix your code step by step:

Root Cause of the Error

The XDMP-ARGTYPE error happens because the second argument to xdmp:document-insert expects a node() (the actual document content), but you're passing $docuri—which is a URI string (xs:anyURI type). That's a classic mix-up between the document's identifier and its content.

Corrected XQuery Code

xquery version "1.0-ml";

for $doc in cts:search(doc(), cts:collection-query('Shipment'))
let $docuri := $doc/base-uri()
let $collections := xdmp:document-get-collections($docuri)
let $permissions := xdmp:document-get-permissions($docuri)
let $target-db := xdmp:database("GTM2_TEST")
return xdmp:invoke-function(
  function() {
    xdmp:document-insert(
      $docuri,
      $doc, <!-- This was $docuri before—fixed to pass the actual document node -->
      <options xmlns="xdmp:document-insert">
        <!-- Use the original document's permissions instead of default -->
        <permissions>{$permissions}</permissions>
        <!-- Merge original collections with new ones (if needed) -->
        <collections>
          {$collections ! <collection>{.}</collection>},
          <collection>GTM2_Shipment</collection>
        </collections>
        <commit>auto</commit>
      </options>
    )
  },
  <!-- Run the function in the context of the target database -->
  <options xmlns="xdmp:eval">
    <database>{$target-db}</database>
  </options>
);

Key Fixes & Improvements

  • Fixed the core error: Swapped $docuri with $doc as the second argument to xdmp:document-insert—this passes the actual document node instead of its URI.
  • Moved target database to xdmp:invoke-function: It's cleaner and more reliable to run the entire insert function in the context of the target database using xdmp:invoke-function's options, rather than specifying it inside document-insert.
  • Preserved original metadata: Now we use the original document's $permissions and $collections instead of hardcoding defaults. If you want to add new collections (like GTM2_Shipment) alongside the original ones, the code merges them as shown.
  • Cleaned up invalid namespace: Removed the invalid xmlns1="xdmp:eval" from the document-insert options—this was unnecessary and could cause parsing issues.
  • Eliminated unused variable: Removed the unused $db-name variable and replaced it with $target-db that's actually used.

Additional Notes

  • If you don't want to preserve the original collections and only want the new ones, replace the <collections> block with just your hardcoded collection elements.
  • For large datasets, consider batching inserts with xdmp:batch-insert or using cts:uris instead of cts:search(doc()) to reduce memory usage (since loading all documents into memory at once can be inefficient).

内容的提问来源于stack exchange,提问作者Rashmita Purkayastha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:27:30