Neo4J:重命名属性键

问题描述 投票:0回答:3

我刚刚开始使用 Neo,并尝试查找有关该主题的先前问题。我需要帮助来重命名其中一个属性键。

我创建了以下节点:

CREATE (Commerce:Category {title:' Commerce', Property:'Category', Owner:'Magic Pie', Manager:'Simple Simon'})

现在想将标题重命名为名称。有办法做到吗?我不想删除该节点,因为有 100 个具有“title”属性的节点。

neo4j cypher
3个回答
30
投票

是的,您想要

SET
拥有旧属性
name
的值的新属性
title
。然后
REMOVE
旧财产
title
。像这样的东西...

MATCH (c:Category)
WHERE c.name IS NULL
SET c.name = c.title
REMOVE c.title

如果您有很多节点,建议小批量执行操作。这是一次限制操作为 10k 的示例。

MATCH (c:Category)
WHERE c.name IS NULL
WITH c
LIMIT 10000
SET c.name = c.title
REMOVE c.title

0
投票

另一种解决方案是使用 APOC 函数:

MATCH (n) WHERE ID(n) = 1234
WITH *, collect(n) AS nodes  // nodes must be converted into a collection first
CALL apoc.refactor.rename.nodeProperty("oldKey ", "newKey", nodes)
// rename doesn't work if the key has strings ^ postfixed in it
YIELD batches, total, timeTaken, committedOperations
RETURN *

如果您不小心在末尾添加了字符串(有可能在创建过程中),则无法通过以下方式访问该属性:

SET n.newKey = n.oldKey
REMOVE n.oldKey

那么你必须使用:

SET n.newKey = n.`oldKey `
REMOVE n.`oldKey `

这有效


0
投票

而且,只是为了添加,您可以使用:

SET c.name = c.title, c.title = null

而不是

SET c.name = c.title
REMOVE c.title

人们对查询的看法和用法不同,适合自己的就用吧。

© www.soinside.com 2019 - 2024. All rights reserved.