Neo4j匹配多个关系 [英] Neo4j match multiple relationships

查看:148
本文介绍了Neo4j匹配多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写查询以获取与集合中所有节点都有关系的节点.例如:

How can I write a query that gets nodes that have relationships to ALL nodes of a set. For example:

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

这将返回与A B有关系的用户.但是我希望与A B有关系的用户.我不知道该怎么做.但是.

This returns users that have relationships to A or B. But I want users that have relationships to A and B. I can't figure out how to do it though.

我需要对任意数量的组执行此操作,而不仅仅是A和B.我使用索引语法的原因是这是来自用户输入的,所以可能是这样:

I need to do this for an arbitrary number of groups, not just A and B. And the reason I'm using index syntax is that this is from user input, so it could be this:

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

我需要返回与所有组都具有M关系的用户.

And I would need to return users that have the M relationship with ALL groups.

推荐答案

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

它甚至可能像这样工作(应该更快)

it might even work like this (should be faster)

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

这篇关于Neo4j匹配多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆