【问题标题】:Gremlin query to count vertices without a certain edgeGremlin 查询以计算没有特定边的顶点
【发布时间】:2020-06-10 18:48:23
【问题描述】:

我在 AWS 中有一个 Neptune 数据库,其图表如下:

所以有一个用户组织了一个聚会并创建了两个邀请。但用户已撤回其中一个邀请。

我正在寻找一个查询来计算尚未撤回的邀请数量(在上述情况下为 1 个)。

我找到了获取邀请的方法(我使用的是Gremlin package for Javascript):

const result = await g
        .V("user#123")
        .inE().hasLabel("createdBy")
        .otherV()
        .toList();

但我似乎找不到任何东西来过滤那些已撤回的邀请。我不认为我可以遍历不是withdrawnBy 的边缘,因为那仍然会返回 2(因为我们有一个 createdBy 和一个 withdrawnBy 边缘)。

有人指点一下吗?我找到了this answer,建议:

gremlin> g.V.filter{!it.bothE('knows').hasNext()}

我认为这是 Groovy,但我使用的是 Javascript(实际上是 Typescript)。我已经尝试过.filter() 函数,但这似乎会引发错误:

.filter(function(this: any) {
    return !this.bothE("isRevokedBy");
})

【问题讨论】:

    标签: graph gremlin amazon-neptune


    【解决方案1】:

    你可以这样做:

    g.V("user#123").as('u').
      in('createdBy').
      not(where(out('withdrawnBy').as('u')))
    

    示例:https://gremlify.com/crjfj88qrtfd8

    但我认为最好在 createdBy 边上添加一个属性,表明它已被撤销,而不是在它们之间添加另一个边。

    像这样:

    g.V("user#123").
      inE('createdBy').not(has('withdrawn', true)).outV()
    

    示例:https://gremlify.com/csfdv6w7325mb

    当您使用 JavaScript 包时,这是可以工作的代码:

    const gremlin = require("gremlin");
    const __ = gremlin.process.statics;
    const traversal = gremlin.process.AnonymousTraversalSource.traversal;
    const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
    const dc = new DriverRemoteConnection("ws://localhost:8182/gremlin");
    const g = traversal().withRemote(dc);
    
    const result = await g
        .V("user#123")
        .in_("createdBy")
        .where(__.not(__.out("withdrawnBy")))
        .toList()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-06
      • 2014-05-15
      • 2020-04-10
      • 1970-01-01
      • 2017-10-24
      • 2021-06-05
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多