I am using below xquery to copy documents from one database to another in MarkLogic. Along with copying my requirement is to remove those documents which are successfully copied. I cannot remove the collection fully as some of the documents may not get copied as they fails to get copied due to template document errors which are present in the target database.
Is there a way to get the list of the documents which are not copied and got failed due to template errors and retain them in the source database and delete the rest of them.
Here is my xquery to copy the documents -
xquery version "1.0-ml";
for $doc in cts:search(doc(), cts:collection-query('Collection1'))
let $docuri := $doc/base-uri()
let $collections := xdmp:document-get-collections($docuri)
let $permissions := xdmp:document-get-permissions($docuri)
let $document-insert-options :=
<options xmlns="xdmp:document-insert">
<permissions>{$permissions}</permissions>
<collections>{
<collection>{$collections}</collection>
}
</collections>
</options>
let $db-name := "STAGING"
let $invoke-function-options :=
<options xmlns="xdmp:eval">
<database>{ xdmp:database($db-name) }</database>
<commit>auto</commit>
</options>
return
xdmp:invoke-function(
function(){ xdmp:document-insert($docuri, $doc, $document-insert-options)},
$invoke-function-options
);