如何通过Cypher中的属性名称字符串访问和更改节点属性值? [英] How to access and mutate node property value by the property name string in Cypher?

查看:253
本文介绍了如何通过Cypher中的属性名称字符串访问和更改节点属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在密码查询中访问和变异节点的属性,其中要访问和变异的属性的名称是未知的字符串值.

My goal is to access and mutate a property of a node in a cypher query where the name of the property to be accessed and mutated is an unknown string value.

例如,请考虑以下命令:

查找包含两个属性的所有节点,以使第一个属性的名称为小写字母,而第一个属性的名称为前者的大写字母表示形式. 然后,将具有小写字符串名称的属性的值传播到具有大写字母名称的属性的值.

Find all nodes containing a two properties such that the name of the first property is lower-case and the name of the latter is the upper-case representation of the former. Then, propagate the value of the property with the lower-case string name to the value of the property with the upper-case name.

这种情况很简单:

MATCH ( node )
WHERE has(node.age) AND has(node.AGE) AND node.age <> node.AGE
SET node.AGE = node.age
RETURN node;

但是我似乎找不到在单个请求中实现一般情况的方法.

But I can't seem to find a way to implement the general case in a single request.

具体地说,我无法:

  1. 使用字符串和值访问节点的属性
  2. 使用字符串和值更改节点的属性

为清楚起见,我将尝试处理一般情况.在无法修改节点属性的地方,我能够为命令生成密码,如果该密码在后续事务中执行,该密码将实现最终目标.

For the sake of clarity, I'll include my attempt to handle the general case. Where I failed to modify the property of the node I was able to generate the cypher for a command that would accomplish my end goal if it were executed in a subsequent transaction.

MERGE ( justToMakeSureOneExists { age: 14, AGE : 140 } ) WITH justToMakeSureOneExists 
MATCH (node) 
WHERE  ANY ( kx IN keys(node) WHERE kx = LOWER(kx) AND ANY ( ky in keys(node) WHERE ky = UPPER(kx) ) )
REMOVE node.name_conflicts // make sure results are current
FOREACH(kx in keys(node) | 
  SET node.name_conflicts 
        = COALESCE(node.name_conflicts,[]) 
        + CASE kx
          WHEN lower(kx)
          THEN []
               +  CASE WHEN any ( ky in keys(node) WHERE ky = upper(kx) )
                  THEN ['match (node) where id(node) = ' + id(node)+ ' and node.' + upper(kx) + ' <>  node.' + kx + '  set node.' + upper(kx) + ' = node.' + kx + ' return node;']
                  ELSE [] END
          ELSE []
          END )
RETURN node,keys(node)

事后思考:似乎可以通过属性名称对节点属性进行突变是很常见的要求,但是缺少对该功能的明显支持使我相信该功能已被省略故意地?如果确实不支持此功能,那么是否有任何文档解释为什么在Neo/Cypher中该方法与推荐的处理方法之间以及为什么存在某些冲突?

Afterthought: It seems like the ability to mutate a node property by property name would be a pretty common requirement, but the lack of obvious support for the feature leads me to believe that the feature was omitted deliberately? If this feature is indeed unsupported is there any documentation to explain why and if there is some conflict between the approach and the recommended way of doing things in Neo/Cypher?

推荐答案

看来,所需的语言功能已在Neo4j 2.3.0中以动态属性"的名称添加到Cypher中.从2.3.0版开始的Cypher文档将以下语法组声明为有效的cypher表达式:

It appears that the desired language feature was added to Cypher in Neo4j 2.3.0 under the name "dynamic property". The Cypher docs from version 2.3.0-up declare the following syntax group as a valid cypher expression:

动态属性:n["prop"]rel[n.city + n.zip]map[coll[0]].

此功能已针对 2.3.0 进行了记录,但此功能不存在先前版本(2.2.9).

This feature is documented for 2.3.0 but is absent from the previous version (2.2.9).

谢谢Neo4j团队!

这篇关于如何通过Cypher中的属性名称字符串访问和更改节点属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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