How to Write CRUD Operations with MarkLogic Database.

female software engineer coding on computer
Reading Time: 4 minutes

CRUD Operations with MarkLogic Database

If you’re looking to store and manage data efficiently and effectively, then MarkLogic Database is an excellent choice. It allows you to easily write CRUD operations (Create, Read, Update, Delete) within the database in order to keep your data organized and secure. In this article we’ll discuss how you can write CRUD operations with MarkLogic Database so that you make the most of your data storage solution.

Introduction to CRUD operations

CRUD is an acronym for Create, Read, Update, and Delete. It is a common set of operations are performing on data in a database. In MarkLogic, CRUD operation using the built-in REST API for performing operations.

Create: To create data in MarkLogic, simply POST to the /v1/documents endpoint. The body of the POST request should contain the document you want to create. MarkLogic will automatically generate a unique ID for the document and return it in the response.

Read: To read data from MarkLogic, use the GET method on the /v1/documents endpoint. You can specify which documents you want to retrieve by including their IDs in the query string. The response will contain the requested documents in JSON format.

Update: To update a document in MarkLogic, use the PUT method on the /v1/documents endpoint. The body of the PUT request should contain the updated document. The ID of the document is includes in the URL path.

Delete: To delete a document from MarkLogic, use the DELETE method on the /v1/documents endpoint. The ID of the document includes in the URL path.

CRUD Operations in MarkLogic Database

MarkLogic is a powerful, flexible and scalable NoSQL database. It supports a wide range of features including full-text search, geospatial search, indexing, aggregation, bitemporal data and more. You can write CRUD operations in MarkLogic using the Java API, REST API or one of the many language bindings.

In this article we’ll focus on writing CRUD operations using the Java API. We’ll assume that you have a basic understanding of Java and the MarkLogic API. If you’re not familiar with either then we recommend checking out our other articles on getting started with MarkLogic and Java.

The first step is to create a new DatabaseClient instance. This is the object that will be used to interact with the database.

DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, 

"Documents");

Once you have a DatabaseClient instance you can start writing CRUD operations. The most common operation is probably creating documents. Using the DocumentManager class we can do it.

    // Create a new document manager

    DocumentManager docMgr = client.newDocumentManager();

    // Create a new JSON document

    String json = "{\"name\": \"John Smith\", \"age\": 25}";
    
    // Write the document to the database

    docMgr.write("/documents/example1.json", new InputStreamHandle

CRUD Operations with MarkLogic Query Language (QL)

MarkLogic is a powerful document-oriented database that supports a wide range of query languages, including its own proprietary language, Query Language (QL). In this article, we’ll take a look at how to write CRUD operations using QL.trg

Create: To create a new document in MarkLogic, we use thexdmp:document-insert function. This function takes two arguments: the first is the URI of the new document, and the second is the XML content insert. For example, to insert a new document with the URI “/user/1” and XML content “John Smith25”, we would use the following code:

xdmp:document-insert("/user/1",

"John Smith25")

Retrieve: To retrieve a document from MarkLogic, we use the xdmp:document-get function. This function takes one argument, which is the URI of the document for retrieved. For example, to retrieve the document with URI “/user/1”, we would use the following code:

xdmp:document-get("/user/1")

Update: To update an existing document in MarkLogic, we use the xdmp:document-update function.

Delete: To delete a document in MarkLogic, we use the xdmp:document-delete function.This function takes one argument, which is the URI of the document for deleted. For example, to retrieve the document with URI “/user/1”, we would use the following code:

xdmp:document-delete("/user/1")

Introduction to SQL and MarkLogic Query Language

SQL (Structured Query Language) is a standard database query language use to manage data stored in relational databases. MarkLogic Query Language (MQL) is a proprietary query language, it designs specifically for use with the MarkLogic NoSQL database.

While SQL is a widely-used and well-understood query language, it is not well suited for use with NoSQL databases like MarkLogic. MQL, on the other hand, was designed specifically for use with MarkLogic and provides a number of features and capabilities that are not available in SQL.

In this article, we’ll take a look at the basic syntax of MQL and how it can to write CRUD operations against a MarkLogic database.

Sending Data to the Server Using REST API

When you are ready to send data to the server using REST API, you need to use a POST request. In the body of your POST request, you will need to include the following:

  • The URI of the resource you are sending data to
  • The JSON representation of the data you are sending
  • The Content-Type header set to application/json

For example, let’s say you have a user resource with the following structure:

{

    "id": 1,

    "name": "John Doe",

    "email": "john@example.com"

}

And you want to create a new user on your MarkLogic Server. You would make a POST request to the /users URI with a body that looks like this:

{

    "name": "Jane Smith",

    "email": "jane@example.com"     // id is not included because it will be

     generated by the server  // name and email are the only required fields        

     for this resource
}

Conclusion

In conclusion, writing CRUD operations for MarkLogic Database is not a difficult task but requires some knowledge and understanding of the technology. By following this tutorial, you have learned how to write your own custom CRUD operations with MarkLogic Database and can now take advantage of its powerful features. With its high performance capabilities, scalability options and rich feature set, MarkLogic Database is an ideal choice for those looking to build robust applications that need reliable data management solutions.

For more functions check this :

https://docs.marklogic.com/xdmp

Leave a Reply