Prev |  | Next

## 15.6. Updating the graph

Cypher can be used for both querying and updating your graph.

### 15.6.1. The Structure of Updating Queries

If you read from the graph, and then update the graph, your query implicitly has two parts — the reading is the first part, and the writing is the second. If your query is read-only, Cypher will be lazy, and not actually pattern match until you ask for the results. Here, the semantics are that all the reading will be done before any writing actually happens. This is very important — without this it’s easy to find cases where the pattern matcher runs into data that is being created by the very same query, and all bets are off. That road leads to Heisenbugs, Brownian motion and cats that are dead and alive at the same time.

First reading, and then writing, is the only pattern where the query parts are implicit — any other order and you have to be explicit about your query parts. The parts are separated using the `WITH` statement. `WITH` is like the event horizon — it’s a barrier between a plan and the finished execution of that plan.

When you want to filter using aggregated data, you have to chain together two reading query parts — the first one does the aggregating, and the second query filters on the results coming from the first one.

    START n=node(...)
    MATCH n-[:friend]-friend
    WITH n, count(friend) as friendsCount
    WHERE friendsCount > 3
    RETURN n, friendsCount

Using `WITH`, you specify how you want the aggregation to happen, and that the aggregation has to be finished before Cypher can start filtering.

You can chain together as many query parts as you have JVM heap for.

### 15.6.2. Returning data

Any query can return data. If your query only reads, it has to return data — it serves no purpose if it doesn’t, and it is not a valid Cypher query. Queries that update the graph don’t have to return anything, but they can.

After all the parts of the query comes one final `RETURN` statement. `RETURN` is not part of any query part — it is a period symbol after an eloquent statement. When `RETURN` is legal, it’s also legal to use `SKIP`/`LIMIT` and `ORDER BY`.

If you return graph elements from a query that has just deleted them — beware, you are holding a pointer that is no longer valid. Operations on that node might fail mysteriously and unpredictably.

Copyright © 2013 Neo Technology

Prev | Up | Next
| Home |