如果不存在,则多次添加步骤格雷姆林



我有一个注入的值数组。如果顶点不存在,我想添加它们。我使用了折叠和合并步骤,但在这种情况下它不起作用,因为我试图对多个顶点进行折叠和合并。由于存在1个顶点,我无法再获得null值,并且合并步骤内部的展开从那时起返回一个值。这导致不存在的顶点尚未添加。

这是我当前的遍历:

const traversal = await g
?.inject([
{ twitterPostId: 'kay', like: true, retweet: false },
{ twitterPostId: 'fay', like: true, retweet: false },
{ twitterPostId: 'nay', like: true, retweet: false },
])
.unfold()
.as('a')
.aggregate('ta')
.V()
.as('b')
.where('b', p.eq('a'))
.by(__.id())
.by('twitterPostId')
.fold()
.coalesce(__.unfold(), __.addV().property(t.id, __.select('ta').unfold().select('twitterPostId')))
.toList();

退货:

[Bn { id: 'kay', label: 'vertex', properties: undefined }]

如果不使用联合,您可以使用我们通常称为";映射注入";。Gremlin确实有点先进,但这里有一个的例子

g.withSideEffect('ids',['3','4','xyz','abc']).
withSideEffect('p',['xyz': ['type':'dog'],'abc':['type':'cat']]).
V('3','4','xyz','abc').
id().fold().as('found').
select('ids').
unfold().
where(without('found')).as('missing').
addV('new-vertex').
property(id,select('missing')).
property('type',select('p').select(select('missing')).select('type'))

该查询将查找一组顶点,找出存在的顶点,其余的将使用名为"p"的映射中的ID值和属性来创建新的顶点。你可以用很多方法建立这种模式,我发现它非常有用,直到合并V和合并E更广泛地可用

您还可以使用查询中的ID列表来检查存在哪些ID。然而,这可能会导致效率低下的查询计划,具体取决于给定的实现:

g.withSideEffect('ids',['3','4','xyz','abc']).
withSideEffect('p',['xyz': ['type':'dog'],'abc':['type':'cat']]).
V().
where(within('ids')).
by(id).
by().
id().fold().as('found').
select('ids').
unfold().
where(without('found')).as('missing').
addV('new-vertex').
property(id,select('missing')).
property('type',select('p').select(select('missing')).select('type'))

这比第一个查询更棘手,因为V步骤不能进行遍历。所以你今天不能在Gremlin做V(select('ids'))

最新更新