Add or get vertex in Azure Cosmos DB Graph API

后端 未结 2 569
闹比i
闹比i 2021-01-25 09:58

Using Gremlin, I can create a vertex in an Azure Cosmos DB graph by issuing

g.addV(\'the-label\').property(\'id\', \'the-id\')

and subsequently

2条回答
  •  走了就别回头了
    2021-01-25 09:59

    The "upsert pattern" is relatively well defined and accepted at this point. It is described here. If you want to extend that to also add an edge, that's possible too:

    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'))
    

    If that looks a bit complex you can definitely simplify with a Gremlin DSL (though I'm not sure that CosmosDB supports Gremlin bytecode at this point). Here's an example with even more complex upsert logic simplified by a DSL. It's discussed in this blog post in more detail.

提交回复
热议问题