如何使用neo4j的Cypher查询语言来匹配一个具有n个以上关系的节点?

kadbb459  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(171)

游戏中回合的节点连接到答案节点。
(:舍入)〈-[:IN_ROUND]-(:答案)
每个Round应该有5个或更少的Answers与它相关,我怀疑我的数据库中存在超过这个数量的节点,我该如何查询这些信息?返回所有:Round节点有超过5个〈-[:IN_ROUND]-关系?

f5emj3cl

f5emj3cl1#

您可以使用WITH clause来计算已连接节点的数量,然后根据此计数进行过滤。
如果要将传入连接指定为Round

MATCH (rnd:Round)<-[:IN_ROUND]-(a)
WITH rnd, count(a) as incomingNodes
WHERE incomingNodes>5
RETRUN rnd

如果要同时计算传入和传出连接:

MATCH (rnd:Round)-[:IN_ROUND]-(a)
WITH rnd, count(a) as connectedNodes
WHERE connectedNodes>5
RETRUN rnd
fnvucqvd

fnvucqvd2#

您可以使用类似于以下内容的内容:

MATCH (n:Round)<-[:IN_ROUND]-(:Answer)
WHERE size((n)<-[:IN_ROUND]-(:Answer)) > 5
RETURN distinct(n)

您可以看到它对以下示例数据有效:

MERGE (a:Round{key: 1})
MERGE (b:Round{key: 2})
MERGE (c:Answer{key: 3})
MERGE (d:Answer{key: 4})
MERGE (e:Answer{key: 5})
MERGE (f:Answer{key: 6})
MERGE (g:Answer{key: 7})
MERGE (h:Answer{key: 8})
MERGE (i:Answer{key: 9})

MERGE (c)-[:IN_ROUND{key:1}]-(a)
MERGE (d)-[:IN_ROUND{key:1}]-(a)
MERGE (e)-[:IN_ROUND{key:1}]-(a)
MERGE (f)-[:IN_ROUND{key:1}]-(a)
MERGE (g)-[:IN_ROUND{key:1}]-(a)
MERGE (h)-[:IN_ROUND{key:1}]-(a)
MERGE (i)-[:IN_ROUND{key:1}]-(b)
MERGE (c)-[:IN_ROUND{key:1}]-(b)
MERGE (e)-[:IN_ROUND{key:1}]-(b)
MERGE (f)-[:IN_ROUND{key:1}]-(b)
MERGE (g)-[:IN_ROUND{key:1}]-(b)

正在返回:

╒═════════╕
│"n"      │
╞═════════╡
│{"key":1}│
└─────────┘

相关问题