Neptune DB中基于Customer属性对Workspace顶点分组的Gremlin查询求助
问题分析与修正方案
你的原查询存在逻辑问题:仅提取了Workspace顶点的customer属性值(即Customer顶点的ID),但未通过该ID关联到对应的Customer顶点,因此无法直接访问Customer的firstName属性,执行后会报错或返回空结果。
正确的Gremlin查询写法
写法一:通过map关联Customer顶点
g.V().hasLabel('Workspace').has('customer'). group(). by(__.values('customer').map(g.V(it)).values('firstName')). by(count())
逻辑说明:
- 先筛选出所有带
customer属性的Workspace顶点 - 对每个Workspace的
customer属性值(Customer ID),用map(g.V(it))找到对应的Customer顶点 - 以Customer的
firstName作为分组键,统计每个分组下的Workspace数量
写法二:用match语句关联顶点(可读性更强)
g.match( __.as('w').hasLabel('Workspace').has('customer'), __.as('c').hasLabel('Customer').where(id().eq(__.select('w').values('customer'))) ).group().by(__.select('c').values('firstName')).by(count('w'))
逻辑说明:
- 通过
match建立Workspace(别名w)和Customer(别名c)的关联关系:Customer的ID等于Workspace的customer属性值 - 以Customer的
firstName为分组键,统计每个分组对应的Workspace数量
内容的提问来源于stack exchange,提问作者Avinash
相关产品推荐
相关产品推荐

