Counting number of Json documents with a where condition on MarkLogic staging DB

Viewed 72

I wanted to know if we can write an XQuery in MarkLogic to get count of JSON documents with a key as "source1"

The staging table has multiple collections with JSON documents with below structure:

{
    "source": "source1",
    "name": "John",
    "DOB": "1-01-1990",
    "load_date": "2021-10-23 10:23:55"
}

I'm trying to get the total number of documents with the source as "source1". i.e,

Select count(*) from data-hub-staging where source='source1'

1 Answers

With a JavaScript module, you can use a cts.jsonPropertyValueQuery() in order to target the documents that have the source property with the value source1, and then use cts.estimate() to get the count of docs:

cts.estimate(cts.jsonPropertyValueQuery("source", "source1"))

and the equivalent XQuery module:

xdmp:estimate(cts:search(doc(), cts:json-property-value-query("source", "source1")))
Related