Select符合条件的所有节点,具有n-n关系的第一条记录

Select all nodes matching a condition, with the first record of a n-n relation

我有 3 种类型的节点:文章、分享、论坛。

一篇文章可以分享到多个版块,所以Share和文章是1-n关系,Share和版块也是1-n关系。

(article:`Article`)<-[:shared_article]-(share:`Share`)-[:shared_in]->(board:`Board`)

我想select一个Article,也加载第一个Board它被共享(A共享有一个创建日期属性)

我如何用密码构建这个查询?

我试过这个:

MATCH (article:`Article`)<-[:shared_article]-(share:`Share`)-[:shared_in]->(board:`Board`)
WHERE article.created_at > ?
RETURN DISTINCT(article), board
ORDER BY board.created_at DESC

但它不起作用。

我也试过这个:

MATCH (article:`Article`)<-[:shared_article]-(share:`Share`)-[:shared_in]->(board:`Board`)
WHERE article.created_at > ?
WITH article, board
ORDER BY board.created_at DESC
RETURN article, head(collect(board)) 

它正在工作,但是...我认为它效率很低,因为我 select 所有节点,而不是第一个节点。

有没有更好的解决方案?

此查询可能对您有用。它按 board.created_at 对不同的 article/board 对进行排序(按 升序 顺序,因为您想要 first 板),聚合每个 article 的所有 board 和每个 article 及其第一个 board 的 returns。我假设提供了一个 time 参数。

MATCH (article:Article)<-[:shared_article]-(share:Share)-[:shared_in]->(board:Board)
WHERE article.created_at > {time}
WITH DISTINCT article, board
ORDER BY board.created_at 
WITH article, COLLECT(board) AS boards
RETURN article, boards[0] AS first_board;

我假设 time 作为 parameter 提供。