MarkLogic Data Hub Service

Fast data integration + improved data governance and security, with no infrastructure to buy or manage.

Learn More


Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up


Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up


Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up

MarkLogic Node.js API -- Working with Binary Documents

by Tamas Piros

In this article we are going to explore the possibilities that Node.js developers have when it comes to managing and working with binary documents in a MarkLogic database.

When I first started at MarkLogic I was blown away by how easily the database can ingest structured, semi-structured and unstructured documents. If you've worked with databases before you know the struggle of persisting binary data -- in MarkLogic that's very easy -- any type of binary can be ingested: PDF, Word document, Powerpoint, MP3, MP4 ... you name it.

There's a lot that could be discussed about how MarkLogic stores binary documents -- there is support for small ( < 512 MB by default ) binaries, large binaries as well as 'external' binaries. To learn more about these please have a look at this page.

Binary nodes and properties

There is also something particular about binary documents: they are not searchable because MarkLogic stores them as binary nodes internally.

So what if you have a large selection of videos or music and you'd like to assign some information to them? Say -- the location of where the video was recorded, or the artist of your favourite track -- information that later on you want to search on.

MarkLogic comes to the rescue! There exists something that we call properties metadata. This piece of metadata is really just an XML document that shares the same URI with a document in a database. The properties metadata then has element-value pairs to store information.

Binaries (and their properties documents) are governed by a role-based security model. You must authenticate as a user that has been assigned a role with a read permission on the binary document in order to be able to view that binary document and search across its properties.

Check out MarkLogic University's series of short video tutorials to learn more about security in MarkLogic.

Going back to the previously mentioned example of the artist of your favourite track you could have a song in your database identified with a URI such as /song/j-balvin-safari.mp3. This document could have the following properties assigned to it with these element-value pairs:

You can add properties metadata during the document insert process or you can also update the document at a later point in time. If a document has properties metadata you can also see that in your Query Console:

You can take a look at these short, bite-sized video tutorials on how to insert documents and how to update documents using the MarkLogic Node.js Client API.

Extracting Metadata from binaries

Some binary documents contain metadata by definition -- think about a Microsoft Office Word document for example -- that has metadata information stored such as the Author, word/character count, last saved time etc. You can extract such metadata in MarkLogic and store it as properties.

There are quite a few ways that this can be achieved in MarkLogic -- have a look at the Search Developer's Guide's chapter on binary documents to learn more.

Inserting documents

If you want to follow this article by way of examples, please clone the following GitHub repository:

To insert a binary document along with some metadata you can use the Node.js Client API in the following way:

Notice the properties property specified as part of the document descriptor -- this is what allows us to assign properties metadata against the binary document.

If you'd like to follow this article and if you have already cloned the GitHub repository you can execute npm run setup to insert some binaries into your database. Please also make sure that you setup the project dependencies as outlined in this readme file.

Now that we have a few binary documents in the database let us continue our discussion on how to display the binaries. There are a few options available for you -- these are all dependent on what size the binary is and whether you'd like to fully or partially read the binary itself.

Displaying Images

To display an image we can use the MarkLogic Node.js Client API's stream result handling pattern. (We also have a promise result handling pattern available.)

Using streams

Generally speaking it's good practice to work with streams when reading binary documents -- and to ask for chunks of data -- these are smaller pieces of data that the database sends to our application. When working with streams in JavaScript we can also use event listeners via the on() method and we can listen on a few events such as data, error and end. The data event gets emitted each time we receive a data chunk. The example below assumes that we have an image loaded into our database already:

To take a look at this example in action please run npm run image.

Displaying Videos using Range requests

When it comes to displaying videos using the Node.js Client API we also need to talk about a few other things including partial HTTP GET statements and Content-Range headers.

What is the difference between streaming a binary in chunks (like in the example we saw before for images) or streaming a binary using the Content-Range header?

Well, the difference is important -- using range requests (via the aforementioned header) retrieves part of a binary document, which means that you can specify a start and end byte to retrieve, and this gives you the option to get retryable, random access to parts of a binary.

Accessing part of a binary

But why is this so important? Well let's think about this for a moment -- let's assume that there exists a video in our database that we'd like to display to the user. First of all, under no circumstances should we download the entire video for the user -- instead we want to download and show the first X amount of bytes -- just enough so that they can start watching the video. As they watch along, we want to download the subsequent parts of the video (i.e. buffer the video). Also, what if the user doesn't want to start from the beginning? We can easily handle that scenario as well by using a Content-Range header.

So how does this work in practice? The MarkLogic Node.js Client API allows you to pass in a range to grab parts of a document:

This would then return exactly the amount of bytes that we have asked for.

Handling ranges from the browser

Now comes the tricky bit. How can we dynamically populate the range array displayed in the previous example? We basically need to check for the existence of the Content-Range header and if it exists we need to get the start and end bytes sent via that header and pass that onto the range array.

It is also very important that in this case we return an HTTP header of 206.

Let's take a look at how this would look like in practice:

If you'd like to see this code in action please execute npm run range.

In the code above we are first calling the db.documents.probe() method, which allows us to actually see the Content-Type of the document which later on we can reuse. Further to this we also check the existence of the Content-Range header, extract the start and end bytes, and build our header that we will return along with the 206 status code.

Once that's done we create a stream by calling with the stream result handling pattern.

It's important to note that when returning the 206 status code some calculation is also required to get the right length of data and pass that in via the appropriate headers. So, for example, if we were to request bytes 0-100 from a file that has a total content length of 1000 we need to specify the following headers:

Remember that when specifying the Content-Range you specify the first and last byte inclusive.

Also notice that we have an else statement to cover scenarios where there are no range headers sent.

Working with metadata

In the beginning of this article I mentioned that there is a way to store metadata against binary documents -- we refer to this as properties.

Using the MarkLogic Node.js Client API it is possible to manage -- insert, update and delete -- the properties metadata. If you have followed along using the scripts found on GitHub -- the setup script has inserted properties metadata for some of the binary documents.

To extract this metadata and display it, all we need to do is to tell our API to retrieve this piece of information:

If you'd like to see this in action, please execute npm run metadata.

Example application

All the previous examples have used separate scripts to retrieve metadata or to stream a video. If you are curious please run the following command npm run app to launch a sample application that uses React and the previously discussed techniques to display information about a video.

The application's source code is available on GitHub.


When creating applications naturally you will encounter binary documents in a variety of formats -- such as JPEGs, PDFs and so on. The MarkLogic Node.js Client API allows you to easily manage and display such binary documents in your application -- and it also helps you to assign and manage metadata.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.


The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • req.headers.range is undefined in the above Handling ranges from the browser code
    • Hi Rakesh - Can you link to the code on GitHub that throws the error and also be a bit more specific with the error message? Thank you!
      • Hi Tamas I was not able to find the range property in req.headers. Please find the node code. ________________________________________________________________________ var http=require('http'); http.createServer(function(req,res){ console.log(req.headers); res.writeHead(200,{'content-Type' : 'text/plain'}); res.end('Hello'); }).listen(2000); ______________________________________________________________________ req.headers printed on console. { host: 'localhost:2000', connection: 'keep-alive', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36', 'upgrade-insecure-requests': '1', accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.8', cookie: 'connect.sid=s%3AsY1et4BrT_o6bdX5cvOtyf26w1zT2eBW.hwiAvN8o5J2LyUUj3TKza8wNOWquWtvsujad3iie%2F%2F0; SessionID=9ba6b479a10f432b' } How do i get range property ? Thank you in advance.
        • You wouldn't get a range property if you're reading a plain text file. Range properties are only received when you work with binary documents: In the case of the sample application provided in the GitHub repository for this post it's being used here: - but note, that the ranges in this case are sent by the browser itself. I hope this helps.
          • Hi Tamas Thank you so much. Kindly Can you share the entire working example of video range requests?
            • Please follow the instructions outlined on this page: