Search code examples
xquerymarklogicroxy

Insert a document into a particular database MarkLogic


I'm setting up a database with roxy, which is different from the "documents" database. I want to insert a config file into that database.

I have this function in xquery ml-1.0

xquery version "1.0-ml";



declare namespace appsetup = "http://kittens.com.au/appsetup";



declare function appsetup:setup-day0($root,$content-db){
    (: some values :)
    let $m := map:new ()
    let $_ := map:put ($m, "Kittens-Forever",xdmp:parse-dateTime("[Y0001]-[M01]-[D01]", "2999-12-31"))
    let $_ := map:put ($m, "Kittens-Never", xdmp:parse-dateTime("[Y0001]-[M01]-[D01]", "1899-12-31"))
    let $_ := map:put ($m, "Kittens-Load-Root", $root)

    return  xdmp:document-insert ("/ksys/smap", <s:map>{$m}</s:map>,
                    (xdmp:permission ("Kittens-role", "read"),
                    xdmp:permission ("Kittens-role", "update")) )

};

The xdmp:document-insert inserts the document in to the default "documents" database. I had a look around, and i couldn't find a way to say load this document into this database. I only found a way to insert the document into a particular forest with xdmp:document-load (https://docs.marklogic.com/xdmp:document-load).

Is there a way for me to say load this thing into this database, preferably just as a xquery parameter?


Solution

  • xdmp:document-insert does not insert into the Documents database by default. It inserts into the database defined in the application server configuration. The idea is that you have an application server that you use to interface with the database.

    Some notes:

    • Use an app server configured for your database.
    • Or use the REST API that has a parameter for this
    • Or use HTTP rewrite rules to dynamically change the database based on a user, URI or query parameter
    • Or use xdmp:eval or its derivatives (spawn, invoke, invoke-function) which all have an option to define which database to use.

    For your particular sample, I would suggest that you use xdmp:invoke-function.