Gremlin Python:如果存在顶点,如何使用coalesce获取顶点,否则插入

Gremlin Python:如果存在顶点,如何使用coalesce获取顶点,否则插入,gremlin,amazon-neptune,gremlinpython,Gremlin,Amazon Neptune,Gremlinpython,如果存在顶点,是否可以使用Gremlincoalesce步骤按id(或属性)选择顶点,否则插入顶点?我尝试使用以下方法执行此操作,但我得到一个'list'对象没有属性'coalesce'错误,我可以看到这是由.fold().next()返回python列表对象引起的: my_vertex = g.V(1).fold().next().coalesce(__.unfold(), g.addV( 'my_label' ).property(

如果存在顶点,是否可以使用Gremlin
coalesce
步骤按id(或属性)选择顶点,否则插入顶点?我尝试使用以下方法执行此操作,但我得到一个
'list'对象没有属性'coalesce'
错误,我可以看到这是由
.fold().next()
返回python列表对象引起的:

    my_vertex = g.V(1).fold().next().coalesce(__.unfold(), g.addV(
         'my_label'
        ).property(
            T.id,
            1
        ).property(
            'test', 'foo'
        ).property(
            'name', 'my_vertex_name'
        ).property(
            'created_timestamp', 1575480467
        ).next()
    )

这样做对性能有什么好处吗?或者我应该在初始顶点查询的hasNext()上将其分解为if/else吗?

您需要从查询中删除
next
。下一步是结束步骤。如果没有
next
,查询应该按照您期望的方式工作,如果V(1)存在,它将不会创建它,否则它将创建它。除此之外,这是执行upsert查询的好方法


如果您使用的是Neptune,请记住ID是字符串,因此它将是V('1')。

我能够通过将下一个()终端步骤移动到遍历的末尾来完成“get If exists,else insert”,如下所示:

my_vertex = g.V(1).fold().next().coalesce(__.unfold(), g.addV(
         'my_label'
        ).property(
            T.id,
            1
        ).property(
            'test', 'foo'
        ).property(
            'name', 'my_vertex_name'
        ).property(
            'created_timestamp', 1575480467
        )
    ).next() //notice that .next was simply moved to the close of the .coalesce step

很清楚,我并不是要执行upsert,而是要执行“get if exists,else insert”。明白了。顺便说一句,当我说删除,我应该说移动到最后。next()是一个终端步骤,就像toList()和iterate()一样,它会导致执行遍历。