在 CosmosDB Graph :“upsert”查询模式中,有一个关于使用
fold()/coalesce()/unfold()
样式模式更新插入顶点并了解顶点是否已添加或获取的精彩讨论:
g.V().has('event','id','1').
fold().
coalesce(unfold().
project('vertex','exists').
by(identity()).
by(constant(true)),
addV('event').property('id','1').
project('vertex','exists').
by(identity()).
by(constant(false)))
该帖子还更新了使用
mergeV()
的更高级模式。我想知道我们是否可以将上面的内容“翻译”为更高级的mergeV()
模式?
非常感谢任何帮助/见解!
新的
mergeV()
模式似乎需要一个列表作为输入,所以我不确定如何从原始查询中映射它。
您可以使用
onMatch
和 onCreate
了解 mergeV
搜索期间发生了什么。这是一种在此基础上生成类似于问题中描述的结果的方法。
g.mergeV([T.id:'1']).
option(Merge.onMatch,sideEffect(constant(true).store('found')).constant([:])).
option(Merge.onCreate,sideEffect(constant(false).store('found')).constant([:])).
project('id','status').
by(id()).
by(select('found').unfold())
由于 ID 为“1”的顶点确实存在,因此运行时,这就是查询返回的内容:
{'id': '1', 'status': True}