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

Gremlin mergeV/mergeE的JavaScript/TypeScript示例求助(Neptune环境)

Amazon Neptune 下 Gremlin mergeV/mergeE 的 JavaScript/TypeScript 示例

mergeV 示例

mergeV用于匹配或创建顶点,核心是通过数组对定义匹配条件(Neptune JS驱动要求此格式,规避Groovy与JS的语法差异),目标顶点不存在则执行onCreate逻辑,存在则执行onMatch逻辑。

const { t } = gremlin.process;
const { onCreate, onMatch } = gremlin.process.merge;

// 定义顶点标识与时间戳
const userId = "user_001";
const now = new Date().toISOString();

// 遍历逻辑:匹配标签为User、userId为user_001的顶点
g.mergeV(
  [t.label, "User"], 
  ["userId", userId]
)
.option(onCreate, {
  userId: userId,
  nickname: "新用户",
  created: now,
  updated: now
})
.option(onMatch, {
  updated: now
})
.next();

mergeE 示例

mergeE用于匹配或创建边,必须明确指定边标签、起始顶点、目标顶点三个核心匹配条件,同样用数组对传递参数,这是解决你报错问题的关键。

const { t } = gremlin.process;
const { from_, to } = gremlin.process.direction;
const { onCreate, onMatch } = gremlin.process.merge;

// 定义边关联的顶点ID、边标签与时间戳
const sourceId = "user_001";
const targetId = "post_100";
const edgeLabel = "likes";
const now = new Date().toISOString();

// 遍历逻辑:匹配从user_001到post_100的likes边
g.mergeE(
  [t.label, edgeLabel],
  [from_, g.V(sourceId)],
  [to, g.V(targetId)]
)
.option(onCreate, {
  created: now,
  updated: now,
  rating: 4.5
})
.option(onMatch, {
  updated: now,
  rating: 4.5
})
.next();

你之前写法的问题解析

  • 直接传递多参数(如mergeE(to, tgt.id)):Neptune JS驱动不支持这种参数形式,必须用数组对定义匹配规则。
  • 使用对象字面量传递方向键:JS中无法直接用from_/to作为对象键(转字符串也无法被Neptune正确解析),必须用[from_, g.V(vertexId)]的数组对格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 13:51:07