You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Gremlin中将vertex_id作为顶点的property添加?

如何用Gremlin为顶点添加vertex_id属性作为property

批量为所有顶点添加vertex_id属性

如果你需要给图中所有顶点都添加vertex_id属性,属性值为顶点自身的ID,可以执行以下Gremlin语句:

g.V().property('vertex_id', id())

如果你的图数据库要求明确指定属性为单值(避免重复添加),可以使用single修饰符:

g.V().property(single, 'vertex_id', id())

为特定标签的顶点添加属性

如果只需要给某类标签(比如User、Product)的顶点添加属性,可通过hasLabel过滤:

g.V().hasLabel('User').property('vertex_id', id())

避免覆盖已存在的vertex_id属性

如果不想覆盖已经存在vertex_id属性的顶点,先通过hasNot过滤无该属性的顶点:

g.V().hasNot('vertex_id').property('vertex_id', id())

验证属性添加结果

执行完添加操作后,可通过以下语句验证是否成功:

// 查看顶点的原始ID和对应的vertex_id属性
g.V().project('original_id', 'vertex_id').by(id()).by('vertex_id')

内容的提问来源于stack exchange,提问作者Thirumal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:50:21