I try to replicate the behaviour of the following SQL query in neo4j
DELETE FROM history
WHERE history.name = $modelName AND id NOT IN (
SELECT history.id
FROM history
JOIN model ON model.id = history.model_id
ORDER BY created DESC
LIMIT 10
)
I tried a lot of different queries, but basically I'm always struggling to incorporate finding the TOP-k elements. That's the closest I got to a solution.
MATCH (h:HISTORY)-[:HISTORY]-(m:MODEL)
WHERE h.name = $modelName
WITH h
MATCH (t:HISTORY)-[:HISTORY]-(m:MODEL)
WITH t ORDER BY t.created DESC LIMIT 10
WHERE NOT h IN t
DELETE h
With that query I get the error expected List<T> but was Node
for the line WITH t ORDER BY t.created DESC LIMIT 10
.
I tried changing it it COLLECT(t) AS t
but then the error is expected Any, Map, Node or Relationship but was List<Node>
.
So I'm pretty much stuck. Any idea how to write this query in Cypher?