简体   繁体   中英

neo4j cypher multi relationship between nodes

for example:

a-[r]->b, there are multi r between the two nodes, each r.userId is unique.
(eg: a-[r:R {userId:"user1"}]->b, (a-[r:R{userId:"user2"}]->b,
and the same for a-[r]->c

And the situation is a-[r]->b has a relationship: r.userId = amdin, but a-[r]->c doesn't have this relationship.

how can i only return c.

i try to create cypher:

"MATCH (a:SomeLabel)-[r:SomeR]->(any:SomeLabel) "
"WHERE id(a)=0 AND r.userId <> \\"admin\\" "
"RETURN any";

but this will also return b ,because a->b has other relationship: r.userId=xxxx

how can i write the cypher to return nodes not inculde user.Id="admin"......

If you not clearly understand what i say,please let me know....i need your help for this case..thanks

I draw a picture below, multi relationship named sr but with different properties (userId is unique), and i want to find all nodes that related to node A, but not contains sr {userId:admin}, i add a red underline there. So as in the picture, node B has the relationship sr {userId:admin}, so i only want to return node C, no node B

在此输入图像描述

For showing simple representations of graph problems, graphgists are really helpful as people can explore the data. I've created one based on your description: http://gist.neo4j.org/?94ef056e41153b116e4f

To your problem, you can collect all usernames involved in the relationships per pair of nodes and filter based on those:

MATCH (a { name:'A' })-[r:sr]->b
WITH a,b, collect(r.name) AS usernames
WHERE NOT 'admin' IN usernames
RETURN a, b

Your question is pretty unclear. My interpretation is that you want to find nodes c that are not connected to a node a with a relationship of type R .

You basically want to do a negative match aka search for a pattern that does not exist. Negative patterns can be retrieved using a where not :

MATCH (a:SomeLabel), (c:SomeLabel)
WHERE ID(a)=0 AND NOT (a)-[:R]->(c)
RETURN c

This returns a list of all SomeLabel nodes not being connected to a .

See http://docs.neo4j.org/chunked/stable/query-where.html#query-where-patterns in the reference manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM