6

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?

Community
  • 1
  • 1
happybayes
  • 321
  • 4
  • 12

4 Answers4

10

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")

Tyler Replogle
  • 1,339
  • 7
  • 13
4

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.

Nikunj Vekariya
  • 833
  • 5
  • 19
3

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")
scotthenninger
  • 3,921
  • 1
  • 15
  • 24
  • i found another function to check if the document exists – happybayes Apr 12 '16 at 22:09
  • 1
    This approach loops through all the documents, reads them, then calls a function for each. Tyler's answer is much more efficient. – Dave Cassel Apr 13 '16 at 11:05
  • This is the only way I found to list all documents from a MarkLogic database. It works in the ML's `queryconsole`. I am just starting out and I needed this to test something. Thank You! – Aleksandar Feb 07 '18 at 23:04
1

Another option to find if the document exists:

xdmp:exists(doc("uri.xml"))
happybayes
  • 321
  • 4
  • 12
  • 2
    This works, but beware that this creates a URI lock. Very useful when you need to check if a uri already exists when creating new documents. – grtjn Apr 13 '16 at 06:20