You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Neo4j(CQL)中查找公共节点属性?附图谱场景查询需求

Neo4j CQL: Finding Common Node Properties & Shared D Nodes

Question 1: How to find properties of common nodes in Neo4j (CQL)?

First, let's clarify what "common nodes" usually refers to—they're nodes that appear in multiple relationship paths or belong to overlapping node groups. Here are practical solutions for different scenarios:

Scenario 1: Nodes shared between two specific nodes (via relationships)

Say you want to pull properties of Post nodes liked by both users Alice and Bob:

MATCH (alice:User {name: 'Alice'})-->(p:Post),
      (bob:User {name: 'Bob'})-->(p:Post)
RETURN p.title AS post_title, p.created_at AS posted_on

This query matches posts connected to both users, then returns their title and creation timestamp.

Scenario 2: Nodes with multiple labels (dual-tagged nodes)

If you're targeting nodes that are both Employee and Manager (and need their details):

MATCH (emp:Employee:Manager)
RETURN emp.full_name AS name, emp.department AS dept

We directly target nodes tagged with both labels and fetch their relevant attributes.

Scenario 3: Common nodes across two disjoint node sets

To find nodes linked to both NodeA and NodeB types (without knowing specific nodes upfront):

MATCH (a:NodeA)-->(common_node)
WITH collect(common_node) AS set_from_a
MATCH (b:NodeB)-->(common_node)
WHERE common_node IN set_from_a
RETURN common_node.id AS node_id, common_node.description AS node_desc

First we collect all nodes connected to NodeA, then filter for those that also connect to NodeB.


Question 2: Given the graph structure A->B, B->C, C->D, B->D, A->D, how to get the value of D nodes shared by A, B, and C?

Looking at your graph, A, B, and C all have direct relationships to D. We need to find D nodes that are connected to all three. Here are two common cases:

Case 1: A, B, C are specific nodes (with unique identifiers)

If A, B, C are individual nodes (e.g., with id properties set to 'A', 'B', 'C'):

MATCH (a {id: 'A'})-->(d:D),
      (b {id: 'B'})-->(d:D),
      (c {id: 'C'})-->(d:D)
RETURN d.value AS shared_d_value

This query matches the specific A, B, C nodes, then finds D nodes that all three point to, returning D's value.

Case 2: A, B, C are node labels (multiple nodes of each type)

If :A, :B, :C are labels (and you want D nodes linked to at least one of each label):

MATCH (n)-->(d:D)
WHERE n:A OR n:B OR n:C
WITH d, collect(DISTINCT labels(n)) AS connected_labels
WHERE ALL(label IN ['A', 'B', 'C'] WHERE label IN connected_labels)
RETURN DISTINCT d.value AS shared_d_value

We first gather all labels of nodes connected to each D, then filter to keep only Ds that have all three labels in their connected node labels. DISTINCT ensures we don't get duplicate values if multiple Ds qualify.


内容的提问来源于stack exchange,提问作者Loyal Lovepreet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:48:31