tubu13
tubu13

Reputation: 934

neo4j match node with multipal nodes connected to him, return them as array

Assuming a node for this example a User has many friends (they are Users aswell) lets assume as well that im looking the user by id which is unique.

How can i query to get one row back with a property of friends as array? Example:

MATCH (user:User {id: "some-id"})-[:FriendsWith]->(friend:User) RETURN user, friend

Now I expected the result to be an array of length one, like this [{user: data, friend: [array of users]}]

But instade I got rows [{user: , friend:}, {user, friend: }] the user was duplicated in each row..

Upvotes: 0

Views: 37

Answers (1)

William Lyon
William Lyon

Reputation: 8556

You can use the collect function to create a collection:

MATCH (user:User {id: "some-id"})-[:FriendsWith]->(friend:User)
RETURN user, collect(friend.name) AS friends

There is an implicit group by when using an aggregation.

Upvotes: 2

Related Questions