I am looking to query Neo4j and come up with a list of connected nodes and the relationship of those connected nodes. More specifically, I am looking to have the results returned in a single list.
For example, consider the following graph:
(user1)-[FOLLOWING]->(user2)
(user1)-[REQUESTED]->(user3)
I am hoping to get a return value back from the query that looks something like (it does not need to look exactly like this, but hopefully something that is close and easy to parse):
{
displayName: "User 2",
username: "user2",
... Other properties from the node ...
relationshipType: "FOLLOWING"
}
-----
{
displayName: "User 3",
username: "user3",
... Other properties from the node ...
relationshipType: "REQUESTED"
}
I have tried a lot of different queries and gotten close but nothing is quite right.
For example, I tried:
MATCH path = (:User {user_id: "user1id"})-[:REQUESTED|FOLLOWING]->(:User)
WITH collect(DISTINCT path) as paths
WITH [p IN paths | {
nodes: [n IN nodes(p) | n.display_name],
usernames: [n IN nodes(p) | n.username],
rels: [r IN relationships(p) |type(r)]
}] as paths
UNWIND paths as result
RETURN result
SKIP 0
LIMIT 3
Which returns:
{
display: ["User 1", "User 2"],
usernames: ["user1", "user2"],
rels: ["FOLLOWING"]
}
-----
{
display: ["User 1", "User 3"],
usernames: ["user1", "user3"],
rels: ["REQUESTED"]
}
I have a couple questions about the query I posted above.
Something else I have tried is:
MATCH (:User {user_id: "user1id"})-[r:REQUESTED|FOLLOWING]->(users:User)
WITH r, collect(users) AS theUsers
WITH [u IN theUsers | {
displayName: u.display_name,
username: u.username
}] as projectedUsers, r
RETURN type(r), projectedUsers
Which returns a table with two columns, but I am unsure if it is guaranteed that the values match up since I have done some manipulation of the users.
When a Cypher request returns a node, the result will contain all the properties of that node. There is no need to generate your own data structure for all the properties (unless a different specific structure is really needed - but it does not look like you have one in mind). When using the neo4j Browser UI, use the Table
or Text
view to see the properties.
Also, it is not clear what kind of result you are trying to get, but here are some possible answers.
Get all desired relationships between User
nodes, then return for each relationship its type and (the properties of) all User
nodes involved (in outbound-relationship order):
MATCH (a:User)-[r:REQUESTED|FOLLOWING]->(b:User)
RETURN TYPE(r) AS t, a, b
Get all desired relationships between User
nodes, then return for each relationship type (the properties of) all distinct User
nodes involved.
MATCH p=(:User)-[r:REQUESTED|FOLLOWING]->(:User)
UNWIND NODES(p) AS us
RETURN TYPE(r) AS t, COLLECT(DISTINCT us) AS users