简体   繁体   English

Neo4j:Cypher:如何按图案去除标签?

[英]Neo4j: Cypher: How to remove labels by pattern?

I have a Neo4j database where each node has labels starting with underscore.我有一个 Neo4j 数据库,其中每个节点都有以下划线开头的标签。

For example, (:User,_User), (:Store,:_Store) etc.例如, (:User,_User), (:Store,:_Store)等。

These underscore labels were generated by Spring Data Neo4j and now I want to get rid of them ( call db.schema() returns them as a separate node in the schema).这些下划线标签是由 Spring 数据 Neo4j 生成的,现在我想摆脱它们( call db.schema()将它们作为模式中的单独节点返回)。

Goal to get only (:User), (:Store) .只获取(:User), (:Store)的目标。

Is there any way to do that with some query?有没有办法通过一些查询来做到这一点?

If you remove the labels that start with underscores (eg _User ) from those nodes and replace them with values without the undescore then calls to db.schema() should no longer return the values.如果您从这些节点中删除以下划线开头的标签(例如_User )并用不带取消划线的值替换它们,则对db.schema()的调用不应再返回这些值。

You could do something like this...你可以做这样的事情......

MATCH (n:_User)
SET n:User 
REMOVE n:_User

Updated answer based on feedback.根据反馈更新答案。 You could do something like this using APOC.你可以使用 APOC 来做这样的事情。

// get all labels that start with underscore
CALL db.labels()
YIELD label AS old_label
WHERE old_label STARTS WITH '_'
WITH old_label, substring(old_label, 1, length(old_label)) AS new_label

// match the nodes for one of the underscore labels
MATCH (n)
WHERE old_label IN labels(n)
WITH old_label, new_label, collect(n) AS relabel_nodes

// call removeLabels with the list of nodes and list od labels to remove
CALL apoc.create.removeLabels(relabel_nodes, [old_label])
YIELD node AS removed_label_node

// call addLabels with the new label to add
WITH removed_label_node, new_label
CALL apoc.create.addLabels(removed_label_node, [new_label])
YIELD node AS added_label_node
RETURN added_label_node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM