Reputation: 680
I am writing an application that stores external data in ArangoDB for further processing inside the application. Let's assume I am talking about Photos
in Photosets
here.
Due to the nature of used APIs, I need to fetch Photosets
befor I can load Photos
. In the Photosets
API reply, there is a list of Photo
IDs that I later use to fetch the Photo
s. So I created an edge collection called photosInSets
and store the edges between Photosets
and Photos
, although the Photos
are not there yet.
Later on, I need to get a list of all needed Photo
s to load them via the API. All IDs are numeric. At the moment, I use the following AQL query to fetch the IDs of all required Photos
:
FOR edge
IN photosInSets
RETURN DISTINCT TO_NUMBER(
SUBSTITUTE(edge._from, "photos/", "")
)
However... this does not look like a nice solution. I'd like to (at least) get rid of the string operation to remove the collection name. What's the nice way to do that?
Upvotes: 0
Views: 173
Reputation: 2349
One way you can find this is with a join on the photosInSets
edge collection back to the photos collection.
Try a query that looks like this:
FOR e IN photoInSets
LET item = (FOR v IN photos FILTER e._from == v._id RETURN v._key)
RETURN item
This joins the _from reference in photoInSets
with the _id back in the photos
collection, then pulls the _key from photos
, which won't have the collection name as part of it.
Have a look at a photo item and you'll see there is _id, _key and _rev as system attributes. It's fine to use the _key value if you want a string, it's not necessary to implement your own unique id unless there is a burning reason why you can't expose _key.
With a little manipulation, you could even return an array of objects stating which photo._key is a member of which photoSet, you'll just have to have two LET commands and return both results. One looking at the Photo, one looking at the photoSet.
I'm not official ArangoDB support, but I'm interested if they have another way of doing this.
Upvotes: 1