How to set collections in query console marklogic

Viewed 149

I am new to marklogic. I want to create a json file with a collection in query console. How to do that? I've already create a database in marklogic server named "Test". And I also inserted serval json files without collections. How to set the collections using XQuery or other methods?

1 Answers

If you have already created the documents, you can set the collection on the documents with xdmp:document-set-collections or xdmp:document-add-collections functions.

Search for the URIs of the documents and then set whatever collection you want:

let $uris := cts:uri-match("*.json")
return xdmp:document-set-collections($uris, "my-collection")

You can set collections and permissions when saving documents to the database with xdmp:document-insert by specifying it in the options parameter.

xdmp:document-insert(
    "/example.xml",
    <a>aaa</a>,
    <options xmlns="xdmp:document-insert">  
      <permissions>{xdmp:default-permissions()}</permissions>
      <collections>{
        <collection>/my/additional/collection</collection>,
        for $coll in xdmp:default-collections()
        return <collection>{$coll}</collection>
      }</collections>
    </options>)
Related