- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Cypher 中的 IF/ELSE
语句的语法是否有任何更新?
我知道CASE
和FOREACH
“黑客”,但它们读起来很难看:)
我想用可选参数做一些事情,例如:
CASE WHEN exists($refs.client) THEN MATCH (cl:client {uuid: $refs.client}) END
...
// and later use it like
CASE WHEN exists(cl) THEN DELETE tcr MERGE (t)-[:references]->(cl) END
// and again in my return
RETURN {
client: CASE WHEN exists(cl) THEN {uuid: cl.uuid} ELSE NULL END,
}
我知道这在上下文中没有多大意义,但我基本上传递了一个 refs 对象,它可能包含也可能不包含参数(或者参数存在并且为 NULL)
我在某处读到,可能有关于 Neo4j 中如何处理“if/else”的更新,所以我真的只是想检查一下,看看是否有人知道处理此类情况的“更好”方法。
目前,我只是在代码中处理所有查询并运行一堆较小的查询,但它需要重复查找来创建和删除引用。我想将其全部移至一个更大的查询中,以便我可以使用变量引用。
再说一次,我知道我可以使用 FOREACH...CASE
,但是当有很多像这样的较小情况时,它就会变得很麻烦。
目前错误是
{ Error: Invalid input 'S': expected 'l/L' (line 7, column 9 (offset: 246))
" CASE true WHEN exists($refs.client) THEN MATCH (cl:client {uuid: $refs.client}) END"
^
我还知道,如果我传回已知值,则可以使用 WITH...CASE
,但不能在其中执行 MATCH
。
想要在查询顶部的 CASE
内执行 MATCH
的原因之一是,如果 refs 上的属性存在,我希望查询失败但 MATCH
没有成功。使用OPTIONAL MATCH
无法实现此目的。
编辑哦,还有...我正在使用MATCH (cl:client {uuid: $refs.client}) WHERE isn't($refs.client)
但我记得它无法正常工作。
编辑我可以做 MATCH...WHERE contains()
但以后如果我不能做 MERGE WHEREexists()
编辑为了说明我为什么询问 IF/ELSE,以下是我正在查看的查询。我对上面的示例进行了修改,这样就不会出错。
MATCH (u:user {uuid: $uid})-[:allowed_to {read: true}]->(c:company {uuid: $cid})
MATCH (t:timesheet {uuid: $tid})<-[:owns]-(:timesheets)<-[:owns]-(u)
// Make sure the incoming references are valid or fail query
// Here, I'd like only do a match IF $refs.client exists and IS NOT NULL. If it is null or does not exist, I don't want the query to fail. OPTIONAL MATCH will not fail if the value is passed in is invalid but will simply return NULL. Which is why IF/ELSE (or CASE) would be helpful here.
MATCH (cl:client {uuid: $refs.client})
MATCH (ca:case {uuid: $refs.case})
MATCH (s:step {uuid: $refs.step})
MATCH (n:note {uuid: $refs.note})
// clone timesheet entry to a revision
CREATE (t)-[:assembled_with]->(r:revision)
SET r = t, r.created_at = $data.updated_at
WITH *
// Get the old references
MATCH (t)-[tcr:references]->(rc:client)
MATCH (t)-[tcar:references]->(rca:case)
MATCH (t)-[tsr:references]->(rs:step)
MATCH (t)-[tnr:references]->(rn:note)
// Copy old references to revision (won't create new relationships with NULL)
MERGE (r)-[:references]->(rc)
MERGE (r)-[:references]->(rca)
MERGE (r)-[:references]->(rs)
MERGE (r)-[:references]->(rn)
// Update the current timesheet with new data
SET t += $data
// If new references are incoming, delete the old ones and update for new ones
DELETE tcr
DELETE tcar
DELETE tsr
DELETE tnr
MERGE (t)-[:references]->(cl)
MERGE (t)-[:references]->(ca)
MERGE (t)-[:references]->(s)
MERGE (t)-[:references]->(n)
WITH *
// Get the new count of revisions
MATCH (t)-[:assembled_with]->(_r:revision)
RETURN {
uuid: t.uuid,
start: t.start,
end: t.end,
description: t.description,
client: CASE WHEN exists(cl.uuid) THEN {uuid: cl.uuid} ELSE NULL END,
case: CASE WHEN exists(ca.uuid) THEN {uuid: ca.uuid} ELSE NULL END,
step: CASE WHEN exists(s.uuid) THEN {uuid: s.uuid} ELSE NULL END,
note: CASE WHEN exists(n.uuid) THEN {uuid: n.uuid} ELSE NULL END,
revisions: count(_r)
}
最佳答案
APOC Procedures刚刚更新支持 conditional cypher execution 。您需要 3.1.3.7 或更高版本(如果使用 Neo4j 3.1.x),或者 3.2.0.3 或更高版本(如果使用 Neo4j 3.2.x)。
以下是您提到的使用新程序的一些案例的示例:
CALL apoc.when($refs.client IS NOT NULL,
"MATCH (cl:client {uuid: refs.client}) RETURN cl", '', {refs:$refs}) YIELD value
WITH value.cl as cl // which might be null...
...
...
CALL apoc.do.when(cl IS NOT NULL,
"DELETE tcr
MERGE (t)-[:references]->(cl)", '', {tcr:tcr, t:t, cl:cl}) YIELD value
...
...
RETURN {
client: cl {.uuid}, ...
}
在您的返回中, map 投影足以满足您的需求,如果 cl
存在,您将获得一个带有 uuid 的对象,如果不存在,则 client
为 null .
关于neo4j - 使用 Cypher Neo4J 进行 IF...ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43481472/
这就是我正在尝试的: match (e:Person) - [r] - (f:Person) where (count(r) > 5 AND count (r) 5 and cnt (m:Perso
每个熟悉 MySQL 的人都可能使用过 mysqldump 命令,它可以生成一个 SQL 语句文件,代表 MySQL 数据库中的模式和数据。这些 SQL 文本文件通常用于多种用途:备份、播种副本、在安
我想了解Spring data neo4j框架中的neo4j dsl推荐。 到目前为止,我曾经创建从 GraphRepository、NamedIndexRepository 等扩展的存储库接口(in
我正在尝试使用 ipython-cypher 在 Python 程序中运行 Neo4j Cypher 查询(并返回 Pandas 数据帧)。使用 IPython Notebook 时,我可以轻松建立连
Neo4j 密码查询collect() 在结果中返回数组。为了迭代它,我们需要将它添加到 arrayList 中。我们之前使用的流程没有帮助我们并抛出异常。 以前的代码:- public Strin
假设您有一个满足以下方案的数据库: (person {name:string, budget:int}) (person)-[:FRIEND]-(person) 如何在 Cypher 中查询以下内容?
{ "identity": 7, "labels": [ "Parent" ], "properties": { "name": "fo
我有一个 Cypher 查询,它返回两个 Tag 节点之间的 TagSynonym 节点: MATCH (t:Tag)(s:Tag) WHERE t.id = {tagId} AND s.id = {
我在一个看似简单的查询上失败了。 算法: 按某个值(desc)对节点进行排序: MATCH (n) WHERE has(n.II_VAL) WITH n, n.II_VAL as ShInflInd
我的 Neo4j 实例上的 Cypher 查询有问题。 我有以下图形结构: (d:Document)-->(t:Token)-->(l:Lemma) Document 可以与许多 Token 有传出关
我需要为给定的关系类型设置某种唯一约束。 (a)-[:RELATION]->(b) 为了实现这一点,我正在使用 CREATE UNIQUE (a)-[:RELATION]->(b) 在并发环境中,如果
我找不到类似的帖子,所以如果你已经知道一个或者我的问题不合适,请告诉我。 我有这个查询 MATCH (t:Taxi {name:'Taxi1813'}) (g2:Grid)-[r:TO*1..
我有以下密码查询,它返回特定节点的所有(进出)关系: START s=node(1) MATCH s(t) RETURN type(r) as RelationshipType, ....... 现在
我正在尝试从全局网络(特定节点的子网络到特定深度)中提取子图。 该网络由标记为 Account 的节点组成,其属性为 iban,关系为 TRANSFER_TO_AGG . 密码语法如下: MATCH
我正在使用 Cypher,我需要构建一个如下所示的查询 (A) ----e1----(B) \ | \ | e3 e2
我的问题是为什么WHERE运算符(operator)的工作速度没有预期的那么快? 考虑我有 7 个节点,标签为 Consumer .这是一些示例数据... MERGE (c:Consumer {mob
我在尝试在我的 Neo4j 中实现搜索时遇到了一个有趣的问题。 D B。我想在任何节点的属性中搜索某个短语(也允许部分匹配)。这必须是通用的并且适用于所有节点类型和标签,因此我无法搜索预定义的属性列表
我是 Neo4j 的新手,目前正在玩这个树结构: 黄色框中的数字是关系CHILD_OF 上名为order 的属性。 我的目标是 a) 通过此属性而不是通过定向关系(例如 LEFT、RIGHT 或 IS
这里属性 r.weight 中的值是一个字符串。我尝试了以下可能性将其转换为整数, INT(r.weight) Integer.parseInt(r.weight) convert(INT,r.wei
我有一个看起来像这样的 neo4j 图: 节点: 蓝色节点:账号 红色节点:电话号码 绿色节点:邮箱 图形设计: (:PhoneNumber) -[:PART_OF]->(:Account) (:Em
我是一名优秀的程序员,十分优秀!