如何在 Gremlin 中编写条件更新语句

How can I write a conditional UPDATE STATEMENT in Gremlin

我如何编写 gremlin 查询以在用户共享产品时将状态值更新为“已共享”。当产品未共享时,我希望状态具有不同的值。我是否需要在同一查询中设置默认值和更新值?

我分享了我的顶点标签和边标签,以及我的顶点图。

NeptuneUserVertexLabel = "user"
NeptuneProductVertexLabel = "product"
NeptuneProductSharedByEdgeLabel = "productsharedby"

这是您可能采用的一种方法的示例,但正如我在上面的评论中提到的那样,一个或多个边的存在确实表示 shared 属性 相同的事情可能表明。

假设这个小图

g.addV('Product').property('shared',false).property('pid','p1').as('p1').
  addV('User').property('name','Name').property('uid','u1').as('u1').
  addE('SharedBy').from('p1').to('u1')   

你可以做到

gremlin> g.V().has('pid','p1').coalesce(filter(out('SharedBy')).property('shared',true),property('shared',false))
==>v[61293]

gremlin> g.V().has('pid','p1').valueMap()
==>[shared:[true],pid:[p1]]   

我们可以删除边缘并重新 运行 查询以证明它有效

gremlin> g.E(61299).drop()

gremlin> g.V().has('pid','p1').coalesce(filter(out('SharedBy')).property('shared',true),property('shared',false))
==>v[61293]

gremlin> g.V().has('pid','p1').valueMap()
==>[shared:[false],pid:[p1]]  

请注意,不需要 属性 已经以某种形式存在。我在示例中以这种方式包含了它,但这完全是可选的。检查 属性 是否存在通常比检查 属性 是否存在更快,因此需要考虑这一点。效率与在图中存储大量额外属性之间的权衡。