Py2neo - "set_node_property"的正确用法

1 投票
1 回答
791 浏览
提问于 2025-04-17 14:33

我正在尝试使用py2neo在一个索引列表中的特定节点上设置一个新属性。我的想法是列表中的第一个节点会获得一个新属性。这个属性的值是固定的,以便将来能找到所有相关的节点。在下面的例子中,"nodez"列表会变化,但第一个项目始终需要这个新属性和固定值。

from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

nodez = ['test1', 'test2', 'test3']
mytestindex = graph_db.get_or_create_index(neo4j.Node, "name")
nodes2 = []
for word in nodez:
    nodes2.append(mytestindex.get_or_create("name", word, {"name": word}))
a = nodes2[0]
newpropkey = "new_property"
newpropvalue = "static_value"
set_node_property(a, newpropkey, newpropvalue)

所以如果下次运行这个程序时,nodez = ['test4', 'test5', 'test6'],那么'test1'和'test4'都会包含这个新属性的值。例如,下面的cypher查询会返回索引"name"中'test1'和'test4'的节点。谢谢大家的帮助!

START a = node:name(new_property="static_value")

1 个回答

2

set_node_property 这个功能只能在批量操作时使用。也就是说,如果你想一次性处理多个节点,就可以用这个方法。在这种情况下,你只需要使用:

a[newpropkey] = newpropvalue

撰写回答