Tom*_*han 1 gremlin azure-cosmosdb
使用Gremlin,我可以通过发出以下命令在Azure Cosmos DB图中创建一个顶点
g.addV('the-label').property('id', 'the-id')
Run Code Online (Sandbox Code Playgroud)
然后使用找到它
g.V('the-label').has('id', 'the-id')
Run Code Online (Sandbox Code Playgroud)
但是,我还没有找到一种发出查询的方法,该查询将在缺少该节点时插入该节点,如果已经存在则仅获取对该节点的引用。有办法吗?
我的具体用例是,我想在两个节点之间添加一条边,而不管这些节点(或该边)是否在单个查询中已经存在。我尝试了这种upsert方法,但是显然Cosmos DB不支持Groovy闭包,因此它不起作用。
在这一点上,“ upsert模式”相对较好地定义和接受。在这里描述。如果您想扩展它以添加一条边缘,那也是可能的:
g.V().has('event','id','1').
fold().
coalesce(unfold(),
addV('event').property('id','1')).as('start').
coalesce(outE('link').has('id','3'),
coalesce(V().has('event','id','2'),
addV('event').property('id','2')).
addE('link').from('start').property('id','3'))
Run Code Online (Sandbox Code Playgroud)
如果看起来有点复杂,则可以使用Gremlin DSL进行简化(尽管我不确定CosmosDB目前是否支持Gremlin字节码)。这是一个通过DSL简化了更为复杂的上插逻辑的示例。此博客文章中对此进行了详细讨论。
| 归档时间: |
|
| 查看次数: |
770 次 |
| 最近记录: |