我正在开发一个小型连接结构,以更好地理解图形数据库的概念,以及学习Neo4J
我的用户很少,他们之间的关系知识如下
我有以下cql来创建Neo4j之间的节点和关系。
CREATE (user1:User {name:"User1",mail:"user1@test.com",mobile: 9000090001})
CREATE (user2:User {name:"User2",mail:"user2@test.com",mobile: 9000090002})
CREATE (user3:User {name:"User3",mail:"user3@test.com",mobile: 9000090003})
CREATE (user4:User {name:"User4",mail:"user4@test.com",mobile: 9000090004})
CREATE (user5:User {name:"User5",mail:"user5@test.com",mobile: 9000090005})
CREATE (user6:User {name:"User6",mail:"user6@test.com",mobile: 9000090006})
CREATE (user7:User {name:"User7",mail:"user7@test.com",mobile: 9000090007})
CREATE (user8:User {name:"User8",mail:"user8@test.com",mobile: 9000090008})
CREATE (user9:User {name:"User9",mail:"user9@test.com",mobile: 9000090009})
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090001 AND user2.mobile = 9000090002 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090001 AND user2.mobile = 9000090003 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090002 AND user2.mobile = 9000090004 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090002 AND user2.mobile = 9000090005 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090003 AND user2.mobile = 9000090006 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090005 AND user2.mobile = 9000090007 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090005 AND user2.mobile = 9000090008 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
MATCH (user1:User), (user2:User) WHERE user1.mobile = 9000090006 AND user2.mobile = 9000090009 CREATE (user1)-[knows:KNOWS ]->(user2) RETURN user1, user2
现在我想为给定用户提取级别2或级别3连接。
例如:
If I ask for level-2 connections for User1 then I should get User4, User5, User6
If I ask for level-3 connections for User1 then I should get User7, User8, User9
我有以下CQL
START levelGraph=node(1)
MATCH path=(user1:User)-[knows:KNOWS*1..]->(user2:User)
WHERE user1.mobile = 9000090001
RETURN user1, user2, length(path) as downlevel
ORDER BY length(path) asc
这个给了我User1所拥有的所有直接或间接连接。任何人都可以指出我应该成为我的cql来实现这个
答案 0 :(得分:0)
您可以修改此
MATCH path=(user1:User)-[knows:KNOWS*1..]->(user2:User)
要获得第二级,它应该像
MATCH path=(user1:User)-[knows:KNOWS*2..2]->(user2:User)
要获得第三级,它应该像
MATCH path=(user1:User)-[knows:KNOWS*3..3]->(user2:User)