How can I return a limited number of nodes (or data from nodes), such as last N nodes, or N nodes, starting at the Mth position, where the number of nodes is not determinable in advance? I know I need to add a "order by" clause to make the query deterministic.
I have just tried a with clause, but with or without a WITH clause, the problem with the Skip clause is that it has to be a static value prior to runtime, so if I have a graph database that has records added every day, to get the last 100 records, I would want to do something like <skip count(n)-100 limit 100/>, but the count function violates the static requirement. Is there another way to do this?
To find the last N records, I would search for all the records created after a specific date/time, order the result by create date in descending order, then limit to N records. This requires you to maintain a 'created on' property; otherwise, you will not know what the 'last' records created are. Note, you can't use 'id' or elementId, as these values are not guaranteed to always increase since they are reused.
You can also you 'lists' to perform the same. You will need to 1) collect the results, 2) take a subset of the list with a starting index (equivalent to skip) and a length (equivalent to limit), and 3) unwind the list if you want rows of records. The disadvantage of this is the collect is an eager operation that may require a lot of memory if you result set is large.
If you are ordering DESC, say for a date, then does it matter how many total nodes there are if you only want the last X? X can be hard-coded, or set by a parameter. e.g. in the Neo4j Browser, run in separate command lines:
:param lastX => 5
MATCH (m:Movie) RETURN m.title, m.released ORDER BY m.released DESC LIMIT $lastX