【问题标题】:Cypher with Neo4j: Order items in collect and not have empty objects使用 Neo4j 的 Cypher:在收集中订购物品并且没有空对象
【发布时间】:2019-03-29 18:31:10
【问题描述】:

我正在尝试编写一个密码查询,在该查询中我记录了一个人的立方体的所有时间。查询工作正常,但我看到的是,如果结果 (Time)-[:RECORDED_ON]-(Cube) 中没有任何关系,那么我在收集中得到 1 条记录,两个值都为空。另外,我想在时间集合DESC中订购日期

这是我的密码:

MATCH(a:App{name:'AllAboutCubes'})<-[:MEMBER_OF]-(p:Cuber) WHERE id(p) = 1234
MATCH(p)<-[:BELONGS_TO {is_active:1}]-(c:Cube)
OPTIONAL MATCH (c)-[:TYPE]->(ct:CubeType)
OPTIONAL MATCH (c)<-[:RECORDED_ON]-(t:Time)-[:RECORDED]-(p) 
WITH c,ct,COLLECT({time:t.time,date:t.date}) as times
RETURN c.name as name, c.manufacturer as manufacturer, ct.type as type, id(c) as cube_id, times as times
ORDER BY manufacturer ASC

但我回来了:

"name"  "manufacturer"  "type"  "cube_id"   "times"
"Weipo" "MoYu"  "2x2"   7452    [{"time":null,"date":null}]
"GTS3M" "MoYu"  "3x3"   7453    [{"time":null,"date":null}]
"MoYu Skewb"    "MoYu"  "Skewb" 7458    [{"time":"12.435","date":1553880809832}]
"WuQue" "QiYi MoFangGe" "4x4"   7459    [{"time":null,"date":null}]
"Valk 3"    "QiYi MoFangGe" "3x3"   7454    [{"time":null,"date":null}]
"Mini Rose Valk 3"  "QiYi MoFangGe" "3x3"   7456    [{"time":null,"date":null}]
"Valk 2"    "QiYi MoFangGe" "2x2"   7450    [{"time":null,"date":null}]
"ChuWen M"  "Supernova" "2x2"   7484    [{"time":null,"date":null}]
"Wingy" "X-Man Designs" "Skewb" 7457    [{"time":"12.255","date":1553881012244},{"time":"13.49","date":1553212800000}]
"Bell"  "X-Man Designs" "Pyraminx"  7478    [{"time":"9.234","date":1553817600000}]
"Ruipo" "YJ"    "2x2"   7451    [{"time":null,"date":null}]
"Little Magic"  "YuXin" "3x3"   7455    [{"time":null,"date":null}]

我不想要 [{"time":null,"date":null}]

有什么想法吗?谢谢,让我知道!

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    这应该按降序对日期进行排序,并在没有日期时为times返回[]:

    MATCH (a:App{name:'AllAboutCubes'})<-[:MEMBER_OF]-(p:Cuber) WHERE id(p) = 1234
    MATCH (p)<-[:BELONGS_TO {is_active:1}]-(c:Cube)
    OPTIONAL MATCH (c)<-[:RECORDED_ON]-(t:Time)-[:RECORDED]-(p)
    WITH c, t ORDER BY t.date DESC
    OPTIONAL MATCH (c)-[:TYPE]->(ct:CubeType)
    WITH c, ct, CASE WHEN t IS NULL THEN [] ELSE COLLECT({time:t.time,date:t.date}) END as times
    RETURN c.name as name, c.manufacturer as manufacturer, ct.type as type, id(c) as cube_id, times
    ORDER BY manufacturer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2014-09-21
      相关资源
      最近更新 更多