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?
推荐答案
最简单的方法是使用 ORDER BY
和 LIMIT
在 DELETE
之前。即:
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屋!