顶点中的 Azure Cosmos Graph 嵌套边缘顶点 属性

Azure Cosmos Graph nest edge vertex in a vertex property

我有两个顶点:

1) 顶点 1:{ id: 1, name: “john” }

2) 顶点 2:{ id: 2, name: “mary” }

从 1 到 2 有一条名为“children”的边。

是否可以像这样使用 gremlin return 2 嵌套在 1 中?

  { 
    id: 1, 
    name: “john”,
    children: { id: 2, name: “mary” }
  }

谢谢!

在@noam621 的惊人帮助下我的解决方案--------------------------------


g.V(1)
.union( valueMap(true),
        project('children').by( coalesce( out('children').valueMap(true).fold() , constant([]))),
        project('parents').by( coalesce( out('parents').valueMap(true).fold() , constant([])))
)
.unfold().group().by(keys).by(select(values))

它 return 是以下对象:

{ 
    id: 1, 
    name: [ “john” ],
    children: [ { id: 2, name: [ “mary” ] } ],
    parents: []
}

.union with project 是将所有对象合并到一个对象中的关键。 valueMap(true).fold() 是获取边缘中所有对象的基础,如果边缘没有 return 任何顶点,则合并有助于使用默认值。

由于某些 Azure Cosmos gremlin 限制,只能将值作为数组值获取。 因此,我在我的应用程序代码中完成了对象格式设置。暂时没问题。

谢谢!

您可以对两个顶点使用 project 步骤:

g.V(1).project('id', 'name', 'children').
    by(id).
    by('name').
    by(out('children').
      project('id', 'name').by(id).
        by('name'))

示例: https://gremlify.com/3j


查询 valueMap:

g.V(1).union(
    valueMap().
      with(WithOptions.tokens).by(unfold()),
    project('children').
      by(out('children').
        valueMap().
          with(WithOptions.tokens).by(unfold()))
  ).unfold().
  group().by(keys).
    by(select(values))

如果 Cosmos 不支持 valueMap().with(WithOptions.tokens),请改用 valueMap(true)