Neo4j匹配多个关系

4

如何编写一个查询来获取与集合中所有节点都有关系的节点。例如:

START n=node:people("username:*"), 
g=node:groups("groupname:A groupname:B") 
MATCH n-[:M]->g 
RETURN n

这将返回与A或B有关系的用户。但我想要与A和B都有关系的用户。不过我不知道该怎么做。
编辑:
我需要对任意数量的组执行此操作,而不仅仅是A和B。我之所以使用索引语法是因为这是来自用户输入的,因此它可能是这样的:
START n=node:people("username:*"), 
g=node:groups("groupname:*") 
MATCH n-[:M]->g 
RETURN n

我需要返回与所有组具有M关系的用户。
2个回答

3
START n=node:people("username:*"), 
g=node:groups("groupname:*") 
with n, collect(g) as groups
MATCH n-[:M]->ug
with n, collect(ug) as user_groups
where ALL(g in groups WHERE g in user_groups)
RETURN n

可能会像这样工作(应该更快)

START n=node:people("username:*"), 
g=node:groups("groupname:*") 
with n, collect(g) as groups
MATCH n-[:M]->ug
WHERE ug in groups
with n, count(*) as found, length(groups) as group_count
WHERE found = group_count
RETURN n

0
将组A和组B分别分配到不同的变量中,然后确保每个匹配都存在于各自的变量中。
START n=node:people("username:*"), 
gA=node:groups("groupname:A"),
gB=node:groups("groupname:B") 
MATCH n-[:M]->gA, n-[:M]->gB 
RETURN n

所以问题在于我需要支持任意数量的组。 - Falmarri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接