Gremlin查询以覆盖顶点 [英] Gremlin query to override the Vertex
问题描述
如何编写将执行的gremlin查询
How to write gremlin query which will do
- 获取现有顶点
- 删除所有现有属性(显然不包括id,label,index属性等)
- 添加具有值的新属性(主要是字符串)
或者有没有其他替代方法可以使用gremlin查询覆盖顶点?
Or is there any alternate way to override the vertex using gremlin query?
推荐答案
以TinkerPop现代图形为例:
Using the TinkerPop modern graph as an example:
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],age:[29]]
并假设完全支持Gremlin,则可以保留"name"属性(即index属性),删除所有其他属性,并在Gremlin的一行中添加新属性,如下所示:
and assuming full Gremlin support you could keep the "name" property (i.e. index property) remove all other properties and add new properties in a single line of Gremlin as follows:
gremlin> g.V().has('person','name','marko').
......1> sideEffect(properties().not(hasKey('name')).drop()).
......2> property('age',33).
......3> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
但是在CosmosDB中,我认为您尚未支持sideEffect()
步骤.诀窍在于,一次遍历就可以做到这一点,即您需要以某种方式副作用" drop()
,并且由于drop()
的行为就像过滤器的所有步骤,因此添加到该步骤之后的任何步骤都不会简单地执行为流中没有任何可操作的内容.
But in CosmosDB, I don't think you yet have support for the sideEffect()
step. The trick is that to do this in one traversal is that you need to "side-effect" the drop()
in some way and since drop()
behaves as a filter everything step, any steps you add after it will simply not execute as there is nothing left in the stream to operate on.
缺少sideEffect()
的一些解决方法包括将union()
与identity()
一起使用:
Some workaround ideas for the lack of sideEffect()
include using union()
with identity()
:
gremlin> g.V().has('person','name','marko').
......1> union(properties().not(hasKey('name')).drop(),
......2> __.identity()).
......3> property('age',33).
......4> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
比sideEffect()
的意图可读性差,但有效.我相信CosmosDB支持identity()
,即使该文件未在其网站上记录.我想,如果您没有identity()
,则只需发挥创意-这是另一种不太好用的方法:
Less readable than the intent of sideEffect()
but effective. I believe CosmosDB supports identity()
even though it is not documented on their web site. If you don't have identity()
you just have to get creative I suppose - here's another way which is even less nice:
gremlin> g.V().has('person','name','marko').as('a').
......1> union(properties().not(hasKey('name')).drop(),
......2> select('a')).
......3> property('age',33).
......4> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
这篇关于Gremlin查询以覆盖顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!