Gremlin 查询计算没有特定边的顶点
Gremlin query to count vertices without a certain edge
我在 AWS 中有一个带有下图的 Neptune 数据库:
所以有一个用户组织了一个派对并创建了两个邀请。但用户已撤回其中一项邀请。
我正在寻找一个查询来计算尚未撤回的邀请数量(在上述情况下为 1)。
我找到了获取邀请的方法(我正在使用 Gremlin package for Javascript):
const result = await g
.V("user#123")
.inE().hasLabel("createdBy")
.otherV()
.toList();
但我似乎找不到任何东西来过滤掉那些已撤回的邀请。我不认为我可以遍历到不是 withdrawnBy
的边,因为那仍然是 return 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");
})
你可以这样做:
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()
我在 AWS 中有一个带有下图的 Neptune 数据库:
所以有一个用户组织了一个派对并创建了两个邀请。但用户已撤回其中一项邀请。
我正在寻找一个查询来计算尚未撤回的邀请数量(在上述情况下为 1)。
我找到了获取邀请的方法(我正在使用 Gremlin package for Javascript):
const result = await g
.V("user#123")
.inE().hasLabel("createdBy")
.otherV()
.toList();
但我似乎找不到任何东西来过滤掉那些已撤回的邀请。我不认为我可以遍历到不是 withdrawnBy
的边,因为那仍然是 return 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");
})
你可以这样做:
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()