假设我有: 两种类型的节点:用户,位置 以及两者之间的关系:费率(用户为“ RATES Place”,可以为同一位置多次评分)
我想获取所有用户对特定地点(提供 placeId )的最新评分(每位用户)列表
因此,从以下情况开始:
USER_1 RATES (createdAt: yesterday) PLACE_1
USER_1 RATES (createdAt: today) PLACE_1
USER_2 RATES (createdAt: yesterday) PLACE_1
USER_2 RATES (createdAt: today) PLACE_1
我要检索:
USER_1 RATES (createdAt: today) PLACE_1
USER_2 RATES (createdAt: today) PLACE_1
通过密码查询是否可行,还是应该更改逻辑并将特定的RATES关系标记为最新?
答案 0 :(得分:1)
MATCH (u)-[r:RATE]->(p)
With u, r, p
order by r.createdAt desc
with u, collect(r) AS rates, p
return u, head(rates), p
首先,获取所有关系并按createdAt
的顺序将它们从最新的(DESC
)降为最近的
然后创建一个由用户使用collect
最后,使用head
返回每个列表的第一个元素以获取最新汇率
答案 1 :(得分:0)
创建示例数据库
CREATE
(a:USER { id:1 } ),
(b:USER { id:2 } ),
(c:PLACE { id:1 } ),
(a)-[:RATE {createdAt: localdatetime("2018-12-20T12:34:00"), rating:5 }]->(c),
(a)-[:RATE {createdAt: localdatetime("2018-12-21T12:34:00"), rating:9 }]->(c),
(b)-[:RATE {createdAt: localdatetime("2018-12-20T12:34:00"), rating:8 }]->(c),
(b)-[:RATE {createdAt: localdatetime("2018-12-21T12:34:00"), rating:7 }]->(c);
查询 (此查询基于this答案)
MATCH (a:USER)-[r:RATE]->(:PLACE)
WITH a, r
ORDER BY a.id DESC
WITH a, collect(r) AS ratings
WITH a, last(ratings) AS rating
RETURN a.id AS user, endNode(rating).id as place, rating.createdAt AS time;
结果
user place time
1 1 2018-12-21T12:34:00
2 1 2018-12-21T12:34:00