I've run into the following issue with this Cypher query. The overall results are returned in an array of objects, as intended. However, the collect() clause is giving me some trouble.
If user has no myFriends and no theirFriends , an empty array should be returned. Otherwise, if myFriends and/or theirFriends has values, it should be a single array of objects of the combined friends, with the id property of the respective friend.
Query:
MATCH (user:User)
WHERE user.id IN ['1', '2', '3']
OPTIONAL MATCH (user)-[:HAS_FRIEND]->(myFriends:User)
OPTIONAL MATCH (user)<-[:HAS_FRIEND]-(theirFriends:User)
OPTIONAL MATCH (user)-[:HAS_TEACHER]->(myTeachers:User)
WITH user, myFriends, friends2
RETURN {
name: user.name,
friends: collect({id: myFriends.id}) + collect({id: theirFriends.id}),
teachers: collect({id: myTeacher.id})
}
Results in:
[
{
name: 'Joe',
friends: [{id: null}, {id: null}],
teachers: [{id: null}]
}, ...
]
Desired result:
[
{
name: 'Joe',
friends: [],
teachers: []
}, {
name: 'Jen',
friends: [{id: '4'}, {id: '6'}, {id: '7'}],
teachers: [{id: '8'}, {id: '9'}]
}
]
.