neo4j order by optional property in descdending order with nulls being at the bottom

Viewed 133

I need to sort nodes by createdAt timestamp in descending order. It is an optional parameter. There are old nodes that don't have this parameter. When I try sorting as suggested in neo4j doc:

MATCH (node:MyTag)
WHERE ...
RETURN node
ORDER BY node.createdAt DESC

I get nodes where createAt does not exist before the ones where it exists. How can I get nodes that have createdAt in descending order first and then have the nodes that don't have createAt field?

Cheers

1 Answers

You could add a field to your RETURN

COALESCE ( node.createdAt, 999999999) AS sortField

that would give you a value to sort on.

Related