【问题标题】:How can i display the all nodes and relationships between two nodes in neo4j?如何在 neo4j 中显示所有节点和两个节点之间的关系?
【发布时间】:2021-01-02 05:29:15
【问题描述】:

创建 Year 和 Month 节点并将它们连接到 Employee 节点。 这是密码:

MERGE (y:Year {year:toInteger(line.YearofJoining)})
MERGE (m:Month {month:line.MonthNamofJoining)})
MERGE (y)-[:MONTH]->(m)

MERGE (a:Employee {empid:line.EmpID, firstname:line.FirstName, lastname:line.LastName, 
gender:line.Gender})
MERGE (m)-[:EMPLOYEE]->(a)

如何显示节点和两个节点之间的关系?例如,如果我选择了两个不同的员工id,这里我想显示两个员工之间的关系是什么(将两个员工作为共同属性名、姓、月、年等)

提前致谢

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    通过此查询,您可以获得两个节点之间的所有关系

    MATCH (n1)-[r]->(n2) 
    RETURN {type: type(r), nodes: {n1: n1{.*}, n2: n2{.*}}}
    
    OR
    
    MATCH (n1)-[r]->(n2) 
    RETURN {type: type(r), nodes: {n1: collect(distinct n1{.*}), n2: collect(distinct n2{.*})}}
    
    

    通过通用属性获取员工:

    MATCH (e:Employee)-[]-(m:Month) 
    return {
      month: m.month, // You can replace it with any property you want to group for example "gender: e.gender"
      employees: collect(distinct e{.*})
    } as byMonth
    

    编辑

    如何知道与给定员工ID相似的其他员工。例如,我在一家公司工作(我的年龄、位置、加入年份作为数据),我想知道公司中具有相似关系的员工,例如相同年龄、加入年份、加入月份、位置等.w.r.t 对我来说

    下面的查询应该可以工作

    match (employee:Employee) where employee.empid= 1
    optional match (employee)-[:EMPLOYEE]-(month:Month{month: 10})-[:EMPLOYEE]-(other:Employee) 
    optional match (other) where other.monthOfJoining = employee.monthOfJoining or other.yearOfJoining = employee.yearOfJoining or other.age = employee.age
    return employee, other
    

    编辑 2

    获取与两个或多个节点相关的所有公共节点的计数

    MATCH (node1:Employee)-->(r)<--(node2:Employee)
    
    with count(r) as maxCountRelation, node1{.*} as e1, node2{.*} as e2
    
    return {commonRelation: maxCountRelation, employees: collect(distinct e1)+collect(distinct e2)} as result order by result.commonRelation desc limit 1
    

    【讨论】:

    • 如何让两个想要的员工共享公共属性?
    • 你所说的“共同属性”是什么意思?
    • 正如您在问题中提到的。员工empid, firstname, lastname, gender只有 4 处房产
    • 例如,列表中任意两名员工的相似入职年份、入职月份、相似名字、姓氏、性别。员工之间如何相互关联。
    • 我已经更新了答案;如有任何疑问,请告诉我
    猜你喜欢
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多