14

I have an array of usernames (eg. ['abc','def','ghi']) to be added under 'user' label in the graph.

Now I first want to check if the username already exists (g.V().hasLabel('user').has('username','def')) and then add only those for which the username property doesn't match under 'user' label.

Also, can this be done in a single gremlin query or groovy script?

I am using titan graph database, tinkerpop3 and gremlin REST server.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
Vipul Sharma
  • 768
  • 3
  • 11
  • 25
  • I answered a similar question here: https://stackoverflow.com/a/76353284/2832598 The method described there allows adding multiple objects in a single request and doesn't require using `fold()`/`unfold()`. – Gyromite May 28 '23 at 19:39

3 Answers3

42

With "scripts" you can always pass a multi-line/command script to the server for processing to get what you want done. This question is then answered with normal programming techniques using variables, if/then statements, etc:

t = g.V().has('person','name','bill')
t.hasNext() ? t.next() : g.addV('person').property('name','bill').next()

or perhaps:

g.V().has('person','name','bill').tryNext().orElseGet{
    g.addV('person').property('name','bill').next()}

But these are groovy scripts and ultimately TinkerPop recommends avoiding scripts and closures in favor of a pure traversal. The general way to handle a "get or create" in a single traversal is to do something like this:

gremlin> g.V().has('person','name','bill').fold().
......1>   coalesce(unfold(), 
......2>            addV('person').property('name','bill'))
==>v[18]

Also see this StackOverflow question for more information on upsert/"get or create" patterns.

UPDATE: As of TinkerPop 3.6.0, the fold()/coalesce()/unfold() pattern has been largely replaced by the new steps of mergeV() and mergeE() which greatly simplify the Gremlin required to do an upsert-like operation. Under 3.6.0 and newer versions, you would write:

g.mergeV([(label): 'person', name: 'bill'])
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • I am making a REST call to gremlin from my nodejs server. So if I have to add 200 users, so should I call the above command 200 times? And if I get an exception or error in few, how to handle it and should I then rollback? Please let me know the correct approach for such scenarios. – Vipul Sharma Sep 07 '17 at 06:01
  • If you are committed to REST and using scripts then just turn my last statement into script that processes a batch of 200 users. `users.each{ g.V().has(..... }` where users is a parameter that has a list of the 200 users. With REST and the batch approach either the whole batch succeeds or it all fails. If you want something else, then modify the script to include a try/catch in the loop to proceed a different way. With scripts you basically have all of groovy to do whatever you need. – stephen mallette Sep 07 '17 at 10:20
1

You can do it directly using :

g.V().has('user','username','def').fold().coalesce(unfold(),addV('user').property('username','def'))
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
0

Just adding into this answer. It is better to use the following idempotent query. Coalesce works like an if-else statement. Refer to, https://spin.atomicobject.com/2021/08/10/idempotent-queries-in-gremlin/ for more information. Also, if you are noticing that the entry is not being saved, make sure you commit the changes using .next().

g.V().hasLabel('user').has('username','def')
    .fold()
    .coalesce(
        __.unfold(), 
        __.addV('user').property('username','def')
    )
    .next()
deirdreamuel
  • 609
  • 8
  • 11