graph - Gremlin 查询以计算没有特定边的顶点
问题描述
我在 AWS 中有一个 Neptune 数据库,其图表如下:
所以有一个用户组织了一个聚会并创建了两个邀请。但是用户已经撤回了其中一个邀请。
我正在寻找一个查询来计算尚未撤回的邀请数量(在上述情况下为 1)。
我找到了一种获取邀请的方法(我正在使用Gremlin 包 Javascript):
const result = await g
.V("user#123")
.inE().hasLabel("createdBy")
.otherV()
.toList();
但我似乎找不到任何东西来过滤那些已撤回的邀请。我不认为我可以遍历那些不是withdrawnBy
因为那仍然会返回 2 的边缘(因为我们有一个createdBy
和一个withdrawnBy
边缘)。
有人有任何指示吗?我找到了这个答案,它建议:
gremlin> g.V.filter{!it.bothE('knows').hasNext()}
我假设这是 Groovy,但我使用的是 Javascript(实际上是 Typescript)。我已经尝试过该.filter()
功能,但这似乎会引发错误:
.filter(function(this: any) {
return !this.bothE("isRevokedBy");
})
解决方案
你可以这样做:
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()
推荐阅读
- python - 如何使用 openpyxl 和 python 在 excel 中编写多张工作表(带 SHEETNAMES)
- c++ - 指向本地范围对象的指针如何仍然调用对象方法?
- rust - 难以理解向量索引操作中的不可变和可变借用
- css - 将 Ant Design Grid 从 24 个部分更改为 80 个部分
- flutter - Flutter:提供程序显示异常,但应用程序在使用默认侦听时运行良好:true
- android - android viewmodel,无法设置值
- sql - 我们可以减少对同一个表的多个连接吗
- angular - owl-carousel 滑块不起作用。当我以角度 8 加载组件时
- node.js - NestJs 在使用 Jest 运行测试时无法编译测试模块
- sparql - 如何使用实体名称使用 SPARQL 查询 Wikidata 并检查替代标签?