Search code examples
marklogic

Get only json documents using Marklogic node.js query builder


In my Marklogic DB I have mixed documents formats. And I would like to get only JSON documents from the collection. I have a query builder statement like this

    const documents = await dbClient.documents
        .query(
            qb
                .where(qb.collection('myCollection'))
                .slice(0, 10)
        )
        .result();

Is it possible to use some filter here to get only JSON documents?


Solution

  • I'm not in a position to quickly test the following, but did walk the docs a bit. Per those docs, you should be able to specify the document format using .withOptions(). That method's search option supports all of cts:search's options, which includes specifying the document format via "format-FORMAT", or format-json, in your case.

    const documents = await dbClient.documents
        .query(
            qb.where(qb.collection('myCollection'))
              .withOptions({search: ['format-json']})
              .slice(0, 10)
        )
        .result();