Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of document uri names from a database marklogic?

Hey I am trying to get the list of all document names /uri from a given MarkLogic database.

I found this line in stackoverflow: How to get total number of documents in Marklogic database?

...which will get the count of documents in a database. I'm not sure how to modify this to list all document URIs.

Also given a document URI I wanted to see if it exists in the database?

I tried the following, but couldn't achieve the same

for $x in xdmp:directory("/myDirectory/", "1")
return
fn:document-uri($x)

I need a Xquery command just like this. I am new to marklogic, can someone help me on this?

like image 447
happybayes Avatar asked Apr 12 '16 21:04

happybayes


3 Answers

If all you need to get from the database is the list of uri use cts:uris(). It will just return the uri and won't require the full documents to be return like fn:doc() would. If the uri lexicon is set to true at the database level the it will just be going off on in an memory index.

If you want to check if a document exists use

fn:doc-available("uri of document")

like image 185
Tyler Replogle Avatar answered Oct 19 '22 01:10

Tyler Replogle


1- If you have used collection then you can use

for $each in collection("collection-name") return fn:document-uri($each)

2- If you have same directory structure then use

for $each in xdmp:directory("/myDirectory/", "infinity") return fn:document-uri($each)

3- If you have not used any of the above case, you can use cts:uris() Please note in order to use cts:uris(), URI Lexicon needs to be enabled.

like image 37
Nikunj Vekariya Avatar answered Oct 19 '22 02:10

Nikunj Vekariya


fn:doc() will return a list of all documents if no parameter is given, or return the document if a URI is given. So to list all URIs in a database, use:

for $x in fn:doc()
   return fn:document-uri($x)

See fn:doc for more. If you want to check whether a document exists, just use fn:doc() with a URI:

fn:doc("/example/uri.xml")
like image 3
scotthenninger Avatar answered Oct 19 '22 01:10

scotthenninger