简体   繁体   中英

Gremlin - how to ensure one vertex only have one inbound edge?

I have person vertex and book vertex connected by owns edge (aka person => owns => book ). How can I ensure one book can only be owned by one person ? In other words, I need a Gremlin query like addE('owns').from(person_1).to(book_1) only if the vertex book_1 has no inbound edge.

This pattern is described in the "Element Existence" recipe and follows the standard fold()/coalesce()/unfold() pattern. Basically, you would so something like:

g.V('book_1_id').as('book_1').
  V('person_1_id').as('person_1').
  coalesce(outE('owns').where(outV().as('book_1')),
           addE('owns').from(`person_1`).to(`book_1`))

If you are using TinkerPop 3.6.x or later you might try using the mergeE() step :

g.mergeE([(from):'book_id_1',(to):'book_id_1',(label):'owns'])
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM