How can i get certain objects from Array in Doc via N1QL


#1

How can query a Doc Docs via N1QL and return data from an Array of Objects. So i am wondering if i can do this in N1Ql directly or if i have to get the whole data and transform the data in nodeJS. Here is what a sample of Data looks like’

{
"Name": "Tom Miller",
"id": "191e13ba-9ac8-4eb5-aa6b-28f56357aca3",
"phones": [
  {
    "guid": "0f8f5ee7-368a-4632-a58e-4581fc0a7912",
    "number": "2135551212",
    "type": "mobile"
  },
  {
    "guid": "752c82a3-6ac0-49ac-ad17-1e6336cb30d6",
    "number": "2135554412",
    "type": "home"
  },
  {
    "guid": "73d016c7-2cb3-42b4-bf2a-c6cbc33b62b3",
    "number": "2135555512",
    "type": "work"
  },
  {
    "guid": "6f5debed-7db6-4466-b484-25944993bd7f",
    "number": "2135556644",
    "type": "fax"
  }
]

}

And this is what i am looking for as far as the output

 {
    "Name": "Tom Miller",
    "id": "191e13ba-9ac8-4eb5-aa6b-28f56357aca3",
    "mobile": "2135551212",
    "work": "2135555512"
  }

#2
SELECT d.Name, d.id,
  (OBJECT v.type:v.`number` FOR v IN d.phones WHEN v.type IN [ "mobile","work"] END).*
FROM default  AS d WHERE .....;

#3

Doesn’t work…

[
{
“code”: 3000,
“msg”: “syntax error - at number”,
“query_from_user”: “SELECT d.Name, d.id,\r\n(OBJECT v.type:v.number FOR v IN d.phones WHEN v.type IN [ “mobile”,“work”] END).*\r\nFROM Contacts d”
}
]


#4

number is reserve keyword. You need to use the back tick ` around it.

SELECT d.Name, d.id,
  (OBJECT v.type:v.`number` FOR v IN d.phones WHEN v.type IN [ "mobile","work"] END).*
FROM default  AS d WHERE .....;