使用gremlin添加边缘(如果不存在) [英] Add edge if not exist using gremlin
问题描述
我正在使用天蓝色的宇宙图数据库.
I'm using cosmos graph db in azure.
有人知道只有在不存在两个顶点之间才可以添加边的方法吗(使用gremlin图查询)?
Does anyone know if there is a way to add an edge between two vertex only if it doesn't exist (using gremlin graph query)?
添加顶点时可以做到这一点,但不能添加边.我从这里:
I can do that when adding a vertex, but not with edges. I took the code to do so from here:
g.Inject(0).coalesce(__.V().has('id', 'idOne'), addV('User').property('id', 'idOne'))
谢谢!
推荐答案
可以对边缘进行处理.模式在概念上与顶点相同,并以coalesce()
为中心.使用现代" TinkerPop玩具图来演示:
It is possible to do with edges. The pattern is conceptually the same as vertices and centers around coalesce()
. Using the "modern" TinkerPop toy graph to demonstrate:
gremlin> g.V().has('person','name','vadas').as('v').
V().has('software','name','ripple').
coalesce(__.inE('created').where(outV().as('v')),
addE('created').from('v').property('weight',0.5))
==>e[13][2-created->5]
在这里,我们在"vadas"和"ripple"之间添加一条边,但前提是该边不存在.这里的关键是检查coalesce()
的第一个参数.
Here we add an edge between "vadas" and "ripple" but only if it doesn't exist already. the key here is the check in the first argument to coalesce()
.
这篇关于使用gremlin添加边缘(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!