happybayes
happybayes

Reputation: 321

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?

Upvotes: 6

Views: 3704

Answers (4)

Nikunj Vekariya
Nikunj Vekariya

Reputation: 833

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.

Upvotes: 4

Tyler Replogle
Tyler Replogle

Reputation: 1339

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

Upvotes: 10

happybayes
happybayes

Reputation: 321

Another option to find if the document exists:

xdmp:exists(doc("uri.xml"))

Upvotes: 1

scotthenninger
scotthenninger

Reputation: 4001

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

Upvotes: 3

Related Questions