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
$docuriwith$docas the second argument toxdmp: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 usingxdmp:invoke-function's options, rather than specifying it insidedocument-insert. - Preserved original metadata: Now we use the original document's
$permissionsand$collectionsinstead of hardcoding defaults. If you want to add new collections (likeGTM2_Shipment) alongside the original ones, the code merges them as shown. - Cleaned up invalid namespace: Removed the invalid
xmlns1="xdmp:eval"from thedocument-insertoptions—this was unnecessary and could cause parsing issues. - Eliminated unused variable: Removed the unused
$db-namevariable and replaced it with$target-dbthat'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-insertor usingcts:urisinstead ofcts:search(doc())to reduce memory usage (since loading all documents into memory at once can be inefficient).
内容的提问来源于stack exchange,提问作者Rashmita Purkayastha
相关产品推荐
相关产品推荐

