Neo4j 密码查询:获取最后 N 个元素 [英] Neo4j cypher query: get last N elements
问题描述
我有一个图表,其中包含用户之间的关系,显示哪些用户访问了另一个人的个人资料,以及何时:
I have a graph that contains a relationship between users, showing what user has visited another's profile, and when:
(visitor:User)-[:VISITED]->(visitee:User)
但我不想存储自开始以来的每次访问.我只想要用户访问过的最后 X 个用户.所以在添加新关系之前,我必须删除最旧的关系,但我不知道如何删除它.我只能得到按日期排序的列表:
But I don't want to store every visit since the beggining of time. I only want the last X users that a user has visited. So before adding a new relationship I must delete the oldest one, but I don't know how to delete it. I can only get a list ordered by date:
MATCH (visitor:User)-[r:VISITED]->(User)
WHERE visitor.user_id = %s
RETURN r
ORDER BY r.date
我需要的是删除此列表中的第一个关系.我该怎么做?
What I need is to delete the first relationship in this list. How can I do that?
推荐答案
最简单的方法是在 DELETE<之前使用
ORDER BY
和 LIMIT
/代码>.即:
The simplest would be to use an ORDER BY
and a LIMIT
before the DELETE
. I.e.:
MATCH (visitor:User)-[r:VISITED]->(User)
WHERE visitor.user_id = %s
WITH r ORDER BY r.date LIMIT 1
DELETE r
一种更有效的机制可能是保留访问的链接列表(这里有一些讨论:http://docs.neo4j.org/chunked/stable/cypherdoc-linked-lists.html)
A more efficient mechanism may be to keep a linked list of visit (there's some discussion on this here: http://docs.neo4j.org/chunked/stable/cypherdoc-linked-lists.html)
这篇关于Neo4j 密码查询:获取最后 N 个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!