To answer why -
When you use an aggregating function like collect, the aggregation (group-by) has to be done by some group by field. In cypher, this group by is implicit based on the fields specified in the WITH or RETURN statement. Any non-aggregated fields used along with an aggregation field become the group-by fields.
So, In your first query, you had -
with collect(n.name) as studs,m
Studs is an aggregated field, m is not. m becomes the default group-by field. Here, it would collect i.e. form a list of students for the value of m who is a tutor. So, you group-by field is the tutor m and it returns a list of students in the studs variable like this - ['stud1', 'stud2','stud3'].
then you had - return studs, collect(m.name) as tutors
Now, the collect would construct a list of tutors using the studs list as the group-by field. Remember, studs is of the form ['stud1', 'stud2','stud3'] which means if there are two rows from the WITH statement where
stud = ['stud1', 'stud2','stud3'] and m = 'tutor1'
stud = ['stud1', 'stud2','stud3'] and m = 'tutor2'
then you final RETURN should give
stud = ['stud1', 'stud2','stud3'] and tutors =[ 'tutor1','tutor2']
Remember, the group-by values have to be exactly the same values, same order.
In your second query,
match (n:Student)-[r:WORKS_WITH]->(m:Tutor)
return collect(n.name) as studs, collect(m.name) as tutors
It only gives me all students in one column and all tutors in the other one instead of giving me all student-tutor pairs.
This is correct. It has no specific group-by fields, so it should simply group all values for both students and tutors and display it.
But the interesting thing is the following :
bug|690x288
For this are you able to display your graph here? Do both students works with all 3 tutors? If not, that might be why.
Also, can you post a sample of the output you want to see?