Neo4j 密码查询:获取最后 N 个元素 [英] Neo4j cypher query: get last N elements

查看:23
本文介绍了Neo4j 密码查询:获取最后 N 个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图表,其中包含用户之间的关系,显示哪些用户访问了另一个人的个人资料,以及何时:

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 BYLIMIT/代码>.即:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆