根据节点属性值向neo4j节点添加约束 [英] Add constraints to neo4j node based on node property value

查看:187
本文介绍了根据节点属性值向neo4j节点添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cypher约束将约束添加到neo4j数据库,并希望创建仅适用于节点类型子集的约束。

I am adding constraints to a neo4j database using Cypher constraints and want to create a constraint which only applies to a subset of a node type.

我可以创建一个约束约束: foo 字段必须存在于 Entity 中,且

I can create a constraint that a foo field must exist on Entity with

创建约束(e:Entity)声明存在(e.foo)

但是我想仅约束具有给定字段的节点。例如

but instead, I want to constrain only nodes with a given field. e.g

创建约束打开(e:Entity {constrain_flag:true)声明存在(e.foo)

例如,我可能有两个节点,例如

For example, I may have two nodes like

(e:Entity {foo: 'bar',constrain_flag:true})

(e:Entity {constrain_flag:false})

我只希望约束 e.foo 必须存在才能应用于 Entity ,其中 constrain_flag = true ,因此这两个都应允许。但是,

I only want the constraint that e.foo must exist to apply to the Entity where constrain_flag = true, so both of these should be allowed. However,

(e:Entity {constrain_flag:false})应该引发异常。

目前有没有办法使用cypher和neo4j?

Is there a way to do this currently with cypher and neo4j?

提前感谢!

推荐答案

除了添加标志属性,您还可以添加其他标签(例如, ConstrainedEntity )到应限制的 Entity 节点。查询可以继续仅使用 Entity 标签。

Instead of adding a flag property, you can just add an additional label (say, ConstrainedEntity) to Entity nodes that should be constrained. Queries can continue to use just the Entity label.

例如:

CREATE CONSTRAINT ON (ce:ConstrainedEntity) ASSERT EXISTS (ce.foo)

要创建已标记 实体

CREATE (e:Entity:ConstrainedEntity {id: 111, foo: 'bar'})

标记现有的实体

MATCH (e:Entity)
WHERE e.id = 123
SET e:ConstrainedEntity

这篇关于根据节点属性值向neo4j节点添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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