Skip to content
andreasronge edited this page Oct 1, 2012 · 3 revisions

With

The ability to chain queries together allows for powerful constructs. In Cypher, the WITH clause is used to pipe the result from one query to the next.

with is also used to separate reading from updating of the graph. Every sub-query of a query must be either read-only or write-only.

There are two different ways of using with method: on paths or on the global context

With on a path/node object

Example of using with on a path

node(1) <=> node(:other_person).ret.with(count) { |_, foaf| foaf > 1 }) >> node

Notice that the first argument in the with block is the node(:other_person) (the object on which the with method operates on). Since the first argument is not used (in the example) in the block we avoid declaring a variable using _ as argument name. The foaf argument corresponds to the count argument - the first argument on the with method. Same as

START v2=node(1) MATCH (v2)--(other_person)-->(v3) WITH other_person,count(*) as v1 WHERE v1 > 1 RETURN other_person

with in the global context

The example above can also be written using the with method on the global context:

(node(1) <=> node(:other_person)) >> node
with(node(:other_person), count) {|_, foaf| foaf > 1}
ret :other_person

Generates: START v2=node(1) MATCH (v2)--(other_person)-->(v3) WITH other_person,count(*) as v1 WHERE v1 > 1 RETURN other_person

Create Nodes

Example:

node.new(:name => 'Andreas', :age => 42)

Same as CREATE (v1 {name : 'Andreas', age : 42}) RETURN v1

Delete

Delete a node:

node(4).del

START v1=node(4) DELETE v1

Delete a relationship

node(3) > rel('r').del > node.del

Same as START v1=node(3) MATCH (v1)-[r]->(v2) DELETE r,v2

Create Paths

Example:

node(1).create_path { |n| n > rel(:friends) > node(2) }

Same as START v1=node(1),v2=node(2) WITH v1 CREATE (v1)-[:friends]->(v2)

More ?

See the RSpecs for more examples see here

Clone this wiki locally