Is it possible to query or sort the document based on the size of an array inside the json document in marklogic?

44 Views Asked by At
{
  "name": "ruby",
  "phone": [
    {
      "type": "mobile",
      "number": 12345678
    },
    {
      "type": "office",
      "number": 2433242
    }
  ]
}

My problem here is, I just want to sort the documents like the one mentioned above based on the size of phone array.

1

There are 1 best solutions below

0
Mads Hansen On

You could create a Template Driven Extraction (TDE) with a column that has the size of the phone array, and then search and sort with the Optic API.

Unfortunately, you cannot simply create a path-range-index with the following XPath, because it isn't an indexable XPath expression:

/array-node('phone')/count(node())