Momin Fakhruddin
Momin Fakhruddin

Reputation: 101

Sorting documents in N1QL using array field of the document

We need to sort documents using array field in the document using N1QL

We have following documents , PCI:

{
  "id": "pci1",
  "_class" = "PCI",
  "productDSL": {
    "parameters": {
      "tpnb": ["02","04"]
    }
  }
},
{
  "id": "pci2",
  "_class" = "PCI",
  "productDSL": {
    "parameters": {
      "tpnb": ["01","02","04"]
    }
  }
},
{
  "id": "pci3",
  "_class" = "PCI",
  "productDSL": {
    "parameters": {
      "tpnb": ["01","02"]
    }
  }
},
{
  "id": "pci4",
  "_class" = "PCI",
  "productDSL": {
    "parameters": {
      "tpnb": ["02","03"]
    }
  }
}

We need to "order by" according to the tpnb field which is an array. So the result should return in the following order

pci3,pci2,pci4,pci1

Upvotes: 0

Views: 138

Answers (1)

vsr
vsr

Reputation: 7414

Try following query. ORDER is based on the array as whole

SELECT id FROM default
WHERE _class = "PCI"
ORDER BY productDSL.parameters.tpnb;

Upvotes: 2

Related Questions