0

如果存在这样的顶点,是否可以使用 Gremlincoalesce步骤按 id(或属性)选择顶点,否则插入顶点?我尝试使用以下方法执行此操作,但出现'list' object has no attribute '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?

4

2 回答 2

2

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

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

于 2019-12-04T19:39:54.997 回答
-1

通过将 next() 终端步骤移动到遍历的末尾,我能够完成“如果存在则获取,否则插入”,如下所示:

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
于 2019-12-12T21:35:52.897 回答