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

4

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?

Gunning answered 12/4, 2016 at 21:19 Comment(1)
Tip: try to ask one question at a time..Adjacent
M
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")
Mesic answered 12/4, 2016 at 21:46 Comment(3)
i found another function to check if the document existsGunning
This approach loops through all the documents, reads them, then calls a function for each. Tyler's answer is much more efficient.Nidifugous
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!Secundine
O
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")

Ory answered 12/4, 2016 at 22:45 Comment(1)
Yes I did thanks. Updated the answer with the correct name. I was using the phone app and was doing it from memory.Ory
F
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.

Framing answered 14/4, 2016 at 13:13 Comment(0)
M
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")
Mesic answered 12/4, 2016 at 21:46 Comment(3)
i found another function to check if the document existsGunning
This approach loops through all the documents, reads them, then calls a function for each. Tyler's answer is much more efficient.Nidifugous
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!Secundine
G
1

Another option to find if the document exists:

xdmp:exists(doc("uri.xml"))
Gunning answered 12/4, 2016 at 22:9 Comment(1)
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.Adjacent

© 2022 - 2024 — McMap. All rights reserved.