To Delete Documents
The Integration Department has completed their collection of Engineering employees to let go. Along with disbanding the department, management has decided to simply delete the records of the terminated employees.
We must now delete the Attrition Department along with the employee documents associated with that department.
An Optic update like this one deletes documents by their URIs:
declareUpdate(); const op = require("/MarkLogic/optic"); op.fromDocUris(cts.orQuery([ cts.documentQuery("/data/departments/Attrition.json"), cts.collectionQuery('https://example.com/content/department/Attrition') ]) ) .remove() .result();
We used this update to delete the Attrition Department document as well as all employee documents in the Attrition collection:
The Data Accessor Function
fromDocUris()
produces a row for each document with the columnuri
containing document URIs:The CTS Function
cts.orQuery()
returns the union of matches that each of its parameter functions finds:cts.documentQuery()
finds the document with the specified URI, our Attrition Department.cts.collectionQuery()
finds all the data from documents in the specified collection, our Attrition collection.
With these CTS functions as parameters,
fromDocUris()
produces a row with theuri
column containing the URI for each document that we want to delete: the Attrition department document and each employee document in the Attrition collection.
The Operator Function
remove()
marks for system deletion the documents whose URIs match the ones infromDocUris()
'suri
column, leaving behind only theuri
column.The Executor Function
result()
executes the update and returns the results as a row sequence.
Here are the first 6 rows of the 1-column result:
{ "uri": "/data/departments/Attrition.json" } { "uri": "/data/employees/f7d055a5-9462-424c-8e9b-13db2878145f.json" } { "uri": "/data/employees/bd5e4f9c-14d5-4f88-89d0-4434fc9318f4.json" } { "uri": "/data/employees/2ef6c49b-ac42-4f83-b887-e31394843ef9.json" } { "uri": "/data/employees/67ba986d-af98-49ee-b731-5b230853ad53.json" } { "uri": "/data/employees/9a4d5d9a-e04e-41a7-a2ac-1bd64f6a2edd.json" }
These are the URIs of the deleted documents.
Before executing this update, you can check that your
fromDocUris()
statement will return the URIs of the documents that you want to delete by commenting outremove()
. The resulting update returns the rows thatfromDocUris()
accesses.Use
execute()
to execute the update without returning the resultant row sequence.With no documents left in it, the Attrition collection automatically vanishes.
You now know how to build Optic updates to insert, update, and delete documents.