How to select value of array as multi result?

n1ql

#1

My docs like this:

{"_id":"list1","type":"list","members":["member1","member2","member3"],"name":"list_name1"}

I want query data by id to get the follows result:

[
  {
    "name": "list_name1",
    "member":"member1"
  },
  {
    "name": "list_name1",
    "member":"member2"
  },
  {
    "name": "list_name1",
    "member":"member3"
  }
]  

How should I do?


#2

here you go;

select a.name, b as member from your_bucket a unnest a.members b;


#3

Thank you very much, It works.


#4

ok,I want to get more complexity result,My docs like this:

{"_id":"list1","type":"list","lev1_members":["member1","member2","member3"],"name":"list_name1","lev2_members":["members4","member5"],"lev3_members":["member6"]}

I want query data by id to get the follows result:

[
  {
    "name": "list_name1",
    "member_id":"member1",
    "leve;":"lev1"
  },
   {
    "name": "list_name1",
    "member_id":"member2",
    "leve;":"lev1"
  },
  {
    "name": "list_name1",
    "member_id":"member3",
    "leve;":"lev1"
  },
  {
    "name": "list_name1",
    "member_id":"member4",
    "leve;":"lev2"
  },
  {
    "name": "list_name1",
    "member_id":"member5",
    "leve;":"lev2"
  },
  {
    "name": "list_name1",
    "member_id":"member6",
    "leve;":"lev3"
  }
]

when I use unnest, I found I can not get any result if I have more than one unnest,How can I do?


#5

@atom_yang,
try this one;

select a.name, lev1, “lev1” leve from default a unnest a.lev1_members lev1
UNION ALL
select b.name, lev2, “lev2” leve from default b unnest b.lev2_members lev2
UNION ALL
select c.name, lev3, “lev3” leve from default c unnest c.lev3_members lev3;


#6

Thanks, It works.but as my test, when I don’t use
use keys["0"]
It will return the result what I want .But If I use
use keys["0"]
It will return nothing.
Thank you again.


#7

correct. that is a residue from my test code. took that out.
thanks