James Newton
James Newton

Reputation: 7114

Cypher query to get all recently updated nodes and relationships

I have a Neo4j database where all the nodes and relationships have an updatedAt property, which is set using TIMESTAMP() in a Cypher query. For the sake of argument, let's imagine that all the nodes were last updated before 144444444444 and at least one relationship was updated after 144444444444.

This cypher query returns no results:

WITH 144444444444 AS timestamp
MATCH (n)
WHERE n.updatedAt >= timestamp
WITH n, timestamp
OPTIONAL MATCH ()-[r]->()
WHERE r.updatedAt >= timestamp
RETURN DISTINCT n, r

The following query (without the timestamp limitation for n) does return the recently updated relationships (which I do want) plus all the nodes (which I don't want):

WITH 144444444444 AS timestamp
MATCH (n)
WITH n, timestamp
OPTIONAL MATCH ()-[r]->()
WHERE r.updatedAt >= timestamp
RETURN DISTINCT n, r

Suspecting that the WHERE n.updatedAt >= timestamp statement might be limiting the possible start and end nodes in the ()-[r]->() statement, I tried the following:

WITH 144444444444 AS timestamp
MATCH (n), (o), (p)
WHERE n.updatedAt >= timestamp
WITH n, o, p, timestamp
OPTIONAL MATCH (o)-[r]->(p)
WHERE r.updatedAt >= timestamp
RETURN DISTINCT n, r

Here, I explicitly allow the start node to be any node at all. However, once again, I do not get the recently updated relationships.

What query can I use to get all the nodes and all the relationships that have been updated since a given timestamp, and none of those that were updated before that time?


EDIT: The following query, using an optional match for n, appears to work:

WITH 144444444444 AS timestamp
OPTIONAL MATCH (n)
WHERE n.updatedAt >= timestamp
WITH n, timestamp
OPTIONAL MATCH ()-[r]->()
WHERE r.updatedAt >= timestamp
RETURN DISTINCT n, r

So my question becomes: why does the query which uses WITH n, o, p, timestamp fail to return the recent relationships?

Upvotes: 0

Views: 1395

Answers (1)

cybersam
cybersam

Reputation: 67044

[EDITED]

It looks like you do not have any nodes with an updatedAt value that is >= 144444444444.

That would explain why the MATCH (n) WHERE n.timestamp >= timestamp queries do not work, but the OPTIONAL MATCH variant does.

A MATCH clause that fails to find a match will cause the entire query to abort and not return any results. But an OPTIONAL MATCH clause always allows the query to proceed.

If I am correct, then this query won't return any rows.

WITH 144444444444 AS timestamp
MATCH (n)
WHERE n.updatedAt >= timestamp
RETURN n;

Upvotes: 1

Related Questions