Basic Xquery CRUD samples equivalent to SQL Select, Insert, Delete or Update

321 views Asked by At

I'm an engineer building products and applications traditionally on the RDBMS systems. I'd like to port the applications and products to MarkLogic, this is very convenient and efficient compared to RDBMS technology. However, there are no adequate CRUD examples for me to take off. Can anyone kindly lead me to the appropriate URI for the same. This will be a great help.

I have a great collaboration suite that is on Javascript+PHP+mySQL, I'd like to port it to Javascript+Xquery+MarkLogic. The point of concern is the lack of understanding on my part due to the vastness of documentation. I can't find the relevant CRUD examples to take off. Kindly help me out.

Thanks in advance. Regards Manish

2

There are 2 answers

2
Manish Deshpande On BEST ANSWER

Thanks Florent Georges for the perfect reply. I'd like to add that all the CRUD scenarios will be complete (at least for my purposes) with: 1. xdmp:document-insert() 2. xdmp:node-insert-child() 3. xdmp:node-insert-before() 4. xdmp:node-insert-after() 5. xdmp:node-insert-child() 6. xdmp:node-delete() 7. xdmp:node-replace() 8. fn:doc() 9. xdmp:document-load()

Really, a lot of thanks to you for pointing me in the right direction. Again, thanks much.

2
Florent Georges On

The atomic piece of information in MarkLogic is the document. A document is identified by a URI (this is its global, unique name in the database). There are many ways to read, create, update and delete documents. But in a nutshell, they all boil down to the following functions:

  • create: a document can be inserted using xdmp:document-insert(), which takes the URI where to create the document, and the document content itself. E.g. xdmp:document-insert('/my/example.xml', <hello>World!</hello>)

  • update: a new version of the document, inserted at the same URI, will override the previous document. So you can simply use xdmp:document-insert() with an existing URI, and the new document you pass it will update the existing one. Instead of replacing the entire document, you can use functions such as xdmp:node-insert-child() to modify sub parts of a document (creating, updating or deleting specific nodes in a document, all of which operations being updates to the document itself)

  • delete: use xdmp:document-delete(), which takes the URI of the document to delete

  • read: this is way more difficult to give one idiomatic way to read. Of course, you can use fn:doc() to read a specific document, given its URI. But as it is very rare in SQL to read one record using its primary key, in MarkLogic usually you use a more complex query to read a document, several documents, specific parts of them, or to return computations based on more complex queries or full text searches. Some examples: fn:doc('/my/example.xml') will return the document with that URI, if any; /hello will return all elements with name hello, at the root of a document (in all documents).

In addition to those building blocks, you have many other ways to interact with documents. Like the REST API. Or existing libraries or applications (e.g. the EXPath Console let you navigate the directories and load documents using a web interface).

But if you want to understand documents in MarkLogic, you will have to play with those functions and understand how to use them, using the Query Console (at http://localhost:8000/qconsole/ on your dev machine). For documentation on a specific function, use the following link (just modify the name of the function): https://docs.marklogic.com/xdmp:document-insert.

(note: this answer takes a couples of shortcuts for the sake of simplicity over absolute technical accuracy, but the concepts are intact)