Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing inner elements in Couchbase with N1QL

Tags:

couchbase

n1ql

Firstly i am sorry if this question is sounding too stupid. But i am recently learning N1QL and need some output for my statement. My bucket name is MultiSiteResponseTime and I am trying to get the result of all the hours by executing "select HourResponsetime.Hour from MultiSiteResponseTime;". But i don't get any result. From what i have understood, if you want to access the inner elements then you have to use it like so HourResponsetime.Hour but i am not sure where i am wrong. Also if you could help me in getting result of a specific key, for example i would like to know the result of ResponseTime present for Hour 1.

{
  "Para": "ResponseTime",
  "Date": "18-04-2016",
  "Qantas": {
    "HourResponsetime": [
      {
        "Hour": 0,
        "ResponseTime": 8
      },
      {
        "Hour": 1,
        "ResponseTime": 9
      },
    ]
  }
}
like image 782
fear_matrix Avatar asked Mar 13 '23 11:03

fear_matrix


1 Answers

This is by no mean a stupid question!

The dotted path syntax "A.B" works well when A is a JSON Object. Your document structure has an array of objects in HoursResponseTime, that's why it doesn't work. The way to go is to use an array index selector with an asterisk: A[*].

Also, the path syntax starts from the root of the document's content. Here you have an intermediary object Qantas, which you should make part of the path.

This gives us:

SELECT Qantas.HourResponsetime[*].Hour FROM MultiSiteResponseTime;

This should return an array of Hours for each document.

like image 169
Simon Baslé Avatar answered May 12 '23 12:05

Simon Baslé