Need to group a Vertex by a Vertex which is saved as a property in that Vertex in Neptune DB using gremlin

huangapple go评论44阅读模式
英文:

Need to group a Vertex by a Vertex which is saved as a property in that Vertex in Neptune DB using gremlin

问题

我有两个顶点 Workspace 和 Customer 在 Neptune 数据库中,Workspace 有 Customer 作为属性。这类似于在关系数据库中在 Workspace 中具有 Customer 作为外键。我想按照 Customer 的一个属性对 Workspace 进行分组。在 Workspace 中,Customer 保存为 Neptune 分配的唯一 id,键为 customer,值为该实体的唯一 id。

在下面的查询中,投影的 customer 基本上是 Workspace 顶点的属性,其值是 Customer 顶点的唯一 id,然后尝试按 Customer 顶点的 firstName 属性进行分组,最后按计数步骤进行分组。我不确定这是否正确的方法。请帮助我解决这个问题。

英文:

I have two vertices Workspace and Customer in Neptune DB and Workspace has Customer as property. It is like having foreign key as Customer in Workspace in RDBMS. I want to group Workspace by one of the Customer's property. In Workspace customer is saved with unique id given by Neptune with key as customer and value as unique id of that entity.

In the below query , projecting customer which is basically an attribute of Workspace vertex having value as unique id of Customer vertex and trying to group by firstName which is a property of Customer vertex and finally grouping by count step. I'm not sure if this is the way. Please help me with this.

g.V().hasLabel('Workspace').
      has('customer').
      project('entity').
        by(values('customer')).
      group().
        by('entity.firstName').
        by(count());

答案1

得分: 1

数据模型没有太多意义。在图中,不需要外键。相反,通常会在 Workspace 和 Customer 顶点之间有一条边。

(Workspace) <- [usedBy] - (Customer)

其中 'usedBy' 将是连接两者的边的类型(或标签)。如果按照这个模式,你可以执行查询如下:

g.V().hasLabel('Workspace').
    group().by(
        in('usedBy').values('entity.firstName'))

然后,如果你想要每个客户的工作空间总数,你可以在末尾添加 count(),如下所示:

g.V().hasLabel('Workspace').
    group().
        by(in('usedBy').values('entity.firstName')).
        by(count())
英文:

The data model doesn't make much sense. In a graph, there's no need for foreign keys. Instead, you would typically have an edge between the Workspace and Customer vertices.

(Workspace) &lt;- [usedBy] - (Customer)

Where 'usedBy' would be the edge type (or label) of the edge connecting the two. If following that pattern, you could do a query such as:

g.V().hasLabel(&#39;Workspace&#39;).
    group().by(
        in(&#39;usedBy&#39;).values(&#39;entity.firstName&#39;))

and then if you wanted the total count of workspaces per customer, you could then add count() on to the end such that:

g.V().hasLabel(&#39;Workspace&#39;).
    group().
        by(in(&#39;usedBy&#39;).values(&#39;entity.firstName&#39;)).
        by(count())

huangapple
  • 本文由 发表于 2023年3月31日 20:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定