What xquery functions I can use to connect with a MarkLogic database and copy the collection from that database to another database located in the same MarkLogic server.
Asked
Active
Viewed 126 times
1
-
When you say "copy the collection", you mean copy all of the documents in that collection, right? You want to find all of the documents in a particular collection and copy them to a different database? – Mads Hansen Jun 24 '22 at 12:11
-
Yes thats right. Copying all documents in one collection from a source database to the other database. – Rashmita Purkayastha Jun 24 '22 at 12:12
1 Answers
0
You can use xdmp:invoke-function()
and specify a different database
in the options.
xquery version "1.0-ml";
for $doc in cts:search(doc(), cts:collection-query("collection-name"))
let $uri := $doc/base-uri()
let $collections := xdmp:document-get-permissions($uri)
let $permissions := xdmp:document-get-collections($uri)
return
xdmp:invoke-function(function() {
xdmp:document-insert($uri, $doc, $permissions, $collections)
},
<options xmlns="xdmp:eval">
<database>{ xdmp:database("GTM2_TEST") }</database>
</options>)
If you have a really large number of documents in the collection, then you might want to look at either xdmp:spawn-function()
, or doing the work with a batch tool, such as CoRB to avoid Expanded Tree Cache errors and/or timeouts from an attempt to process them all in one giant transaction.

Mads Hansen
- 63,927
- 12
- 112
- 147
-
xquery version "1.0-ml"; for $doc in cts:search(doc(), cts:collection-query("GTM2_Shipment")) let $uri := $doc/base-uri() let $db-name := "GTM2_FINAL" return xdmp:invoke-function(function() { xdmp:document-insert($uri, $doc) },
xdmp:database("$db-name") -
It needs the ID of the database. You need to wrap the call to xdmp:database with curly braces, to create a code block to execute and return the ID. Right now the function call is being sent as a literal string. – Mads Hansen Jun 29 '22 at 10:35
-
I have tried both these and it gaves me same invalid option error -
xdmp:database{"GTM2_TEST"} xdmp:database("GTM2_TEST") xdmp:database(GTM2_TEST) Missing Context item when I usedxdmp:database{GTM2_TEST} – Rashmita Purkayastha Jun 29 '22 at 11:16 -
Sorry that I wasn't clear. The curly braces need to wrap around the function call: `
{ xdmp:database("GTM2_TEST") } `. I updated the answer to include it with the enclosed expression. – Mads Hansen Jun 29 '22 at 11:46 -
It work with the above syntax. However the collection and permission things doesnot gets copied. I am trying this code- | – Rashmita Purkayastha Jun 29 '22 at 14:08
-
-
https://stackoverflow.com/q/72803218/18123235 @MadsHansen - Can you please check if possible. – Rashmita Purkayastha Jun 29 '22 at 14:36