Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are collections in Cypher / Neo4J?

Tags:

neo4j

cypher

I do not really understand what is the difference of collections from other type of output in Cypher. Can somebody explain this to me, please?

For instance the query

match (c:Context) where c.name="health" or c.name="opinion" return collect(c);

returns 1 row, while the query

match (c:Context) where c.name="health" or c.name="opinion" return c;

returns 6 rows (I have 6 nodes in my database that match the criteria).

This seems to be the only difference.

So then, is it just about the way the data is represented? Or is there some sort of advantage to use collections?

Thank you for your help!

like image 916
Aerodynamika Avatar asked Feb 26 '14 10:02

Aerodynamika


People also ask

What is schema in Neo4j?

A schema in Neo4j refers to indexes and constraints. Neo4j is often described as schema optional, meaning that it is not necessary to create indexes and constraints. You can create data — nodes, relationships and properties — without defining a schema up front.

How do you count nodes in Cypher?

Using count(*) to return the number of nodes The function count(*) can be used to return the number of nodes; for example, the number of nodes connected to some node n . The labels and age property of the start node n and the number of nodes related to n are returned.

What is Cypher in Neo4j?

Cypher is Neo4j's graph query language that lets you retrieve data from the graph. It is like SQL for graphs, and was inspired by SQL so it lets you focus on what data you want out of the graph (not how to go get it).

What are variables in Neo4j?

When you reference parts of a pattern or a query, you do so by naming them. The names you give the different parts are called variables.


1 Answers

Collections return the entities in an array, instead of an individual "row" for each result. The benefit of this is, for example: I want to get all addresses associated to a contact.

 Match (c:Contact)-[:AddressRelation]->(a:Address)
 return c,collect(a)

This would return a group of addresses for each contact, whereas without collect, it would return duplicate contact items (one for each address they have)

Collect returns something like this:

row = { name:"fred" } , [{address1},{address2},...]

Without collect:

row = { name:"fred"} , {address1}

row = { name:"fred"} , {address2}

...etc.

There are a lot of other things you can do, like return a property in an array, loop through each node in a foreach loop, etc.

like image 83
dcinzona Avatar answered Oct 04 '22 17:10

dcinzona