-
Notifications
You must be signed in to change notification settings - Fork 274
Neo4j::Cypher Return
Unlike the original cypher language you can specify a return value at any place in the DSL. If you don't specify a return value it will try to return the last value evaluated in the DSL.
node(2)
node(3)
Will generate the following Cypher string: START v1=node(2),v2=node(3) RETURN v2
If you want to return both nodes in the example above you can either return an array or use the ret
method (see below).
Example of returning an array
[node(2), node(3)]
Will generate: START v1=node(2),v2=node(3) RETURN v1,v2
Example, specify the variable names (see Neo4j::Cypher-Start
[node(2).as(:x), node(3).as(:y)]
Will generate: START x=node(2),y=node(3) RETURN x,y
Instead of returning an Ruby array you can specify the return values using the ret
method.
ret(node(1), node(2))
Will generate: START v1=node(1),v2=node(2) RETURN v1,v2
Notice that you don't have to use the ret method in the last evaluated expression. A silly example:
ret(node(2))
node(3)
Will return: START v1=node(2),v2=node(3) RETURN v1
Ruby symbols corresponds to the cypher variable names.
node(2).as(:foo) >> :bar; :bar
Will generate: START foo=node(2) MATCH (foo)-->(bar) RETURN bar
The symbols can also be used in the ret method, example
node(2) >> :x >> :y
ret :x, :y
Will generate: START v1=node(2) MATCH (v1)-->(x)-->(y) RETURN x,y
The ret
method is also available on paths, nodes, relationships and properties !
This allows you to specify the return value immediately.
Instead of the two line example above you can write that in one line like this:
node(2) >> node(:x).ret >> node(:y).ret
Will generate: START v1=node(2) MATCH (v1)-->(x)-->(y) RETURN x,y
Read first how aggregation works in neo4j here
COUNT is used to count the number of rows. COUNT can be used in two forms — COUNT(*) which just counts the number of matching rows, and COUNT(), which counts the number of non-null values in .
Example, count my friends
node(1).outgoing(:friends).count
Same as START v2=node(1) MATCH (v2)-[:
friends]->(v1) RETURN count(v1)
Example: To count the number of nodes, for example the number of nodes connected to one node, you can use
ret node(1).outgoing,count
Same as START v1=node(1) MATCH (v1)-->(v2) RETURN v2,count(*)
TODO write docs or see the rspecs
TODO write docs, or see the rspecs
WARNING: Much of the information in this wiki is out of date. We are in the process of moving things to readthedocs
- Project Introduction
- Neo4j::ActiveNode
- Neo4j::ActiveRel
- Search and Scope
- Validation, Uniqueness, and Case Sensitivity
- Indexing VS Legacy Indexing
- Optimized Methods
- Inheritance
- Core: Nodes & Rels
- Introduction
- Persistence
- Find : Lucene
- Relationships
- Third Party Gems & extensions
- Scaffolding & Generators
- HA Cluster