How to perform a bidirectional upsert in ArangoDB

84 Views Asked by At

I'm working with a dataset similar to ArangoDB official "friendship" example, except I'm adding a "weight" concept on the Edge Collection. Like so :

People

[
  { "_id": "people/100", "_key": "100", "name": "John" },
  { "_id": "people/101", "_key": "101", "name": "Fred" },
  { "_id": "people/102", "_key": "102", "name": "Jacob" },
  { "_id": "people/103", "_key": "103", "name": "Ethan" }
]

Friendship

[
  { "_from": "people/100", "_to": "people/101", "weight": 27 },
  { "_from": "people/103", "_to": "people/102", "weight": 31 },
  { "_from": "people/102", "_to": "people/100", "weight": 12 },
  { "_from": "people/101", "_to": "people/103", "weight": 56 }
]

I want to write a function that, when someone interacts with someone else, UPSERTs the Friendship between the two (incrementing the weight by 1 if it existed before, or initializing with a weight of 1 if it's new).

The trouble is, when executing that function, I have now clue on which direction the friendship was initialized, thus I cannot really use an upsert. So 2 questions here :

  1. Is there any way to make an upsert on an edge with "bidirectional" filter ?

Like so, but bidirectional

UPSERT {
  // HERE, I BASICALLY WAN'T TO IGNORE THE SIDE
  _from: ${people1}, _to: ${people2}
}
INSERT {
  _from: ${people1}, _to: ${people2}, weight: 1
}
UPDATE {
  weight: OLD.weight + 1
}
IN ${friendshipCollection}
RETURN NEW 
  1. Instead of trying to "select the friendship, no matter the direction"; should I rather actually duplicate the friendship on both directions (and constantly maintain / update it) ?
0

There are 0 best solutions below