neo4j how to return two nodes linked by edge

时间:2016-07-11 19:38:24

标签: neo4j cypher

I have the following nodes link by a edge

MATCH (user)-[:CreatesChat]-(chatitems)

Node user has the property user.id Node chatitems has the property chatitems.id

I want to recover the user.id and the chatitems.id

I tried

MATCH (user)-[:CreatesChat]-(chatitems)
WITH user as users ,chatitems as chats ORDER BY chatitems.id DESC LIMIT 10
WITH collect(chats) AS chats1
with collect(users) as users1
UNWIND chats1 AS chatid
unwind users1 as userid
return chatid,userid

But I get the error

users not defined (line 4, column 14 (offset: 158))
"with collect(users) as users1"
              ^

How can I fix this error?

1 个答案:

答案 0 :(得分:2)

Every time you write a WITH you are resetting everything you have returned up to that point, so strictly speaking you would fix this error by making your second WITH statement

WITH users, collect(chats) as chats1

And then you'd have to make your third

WITH users, chats1, collect(users) as users1

and so on. But this all strikes me as overkill. It sounds like you simply want:

MATCH (user)-[:CreatesChat]-(chatitems)
RETURN user.id, chatitems.id