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

Gremlin中如何将注入数组选中值设为property步骤的值?

解决方案

原查询存在两个核心问题:

  • 执行.select('twitterPostId').as('t')后,遍历器当前元素变为twitterPostId的具体值,丢失了注入对象中like、retweet等其他属性信息
  • property步骤内部无法直接通过__.select('t')访问之前的别名,因为addV()会切换遍历器上下文

可以通过保留注入对象的完整上下文来解决,修正后的查询如下:

await g
  ?.inject([
      { twitterPostId: 'tay', like: true, retweet: false },
      { twitterPostId: 'fay', like: true, retweet: false },
  ])
  .unfold()
  .as('a') // 保留完整注入对象的别名
  .V()
  .hasId(__.select('a').by('twitterPostId')) // 从别名'a'中取twitterPostId作为查询ID
  .fold()
  .coalesce(
      __.unfold(), // 若找到顶点则直接返回
      __.addV()
          .property('id', __.select('a').by('twitterPostId')) // 从'a'中取ID设置顶点ID
          .property('like', __.select('a').by('like')) // 设置like属性
          .property('retweet', __.select('a').by('retweet')) // 设置retweet属性
  )
  .next();

关键调整说明

  • 全程保留注入对象的别名a,确保所有属性都能被访问
  • 在hasId和property步骤中,通过__.select('a').by('属性名')的方式,从原始注入对象中提取对应值,避免上下文切换导致的访问失败
  • coalesce逻辑保持不变:优先返回已存在的顶点,不存在则创建新顶点并设置所有属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 05:36:17