I'm new to ArangoDB but I've been using the Arango-Net-Standard driver with some success to add nodes and edges to a graph in C#.
I can create Documents in bulk using this call
await conn.Document.PostDocumentsAsync<Document>("my_document_collection", allDocuments, null)
But I can't see an obvious way of creating edges in bulk. A complicating factor is that I don't know the Document id's so have to do a lookup on a property (uuid) that I do know.
This AQL works in the web front end. But I'm not sure how to use the ArangoDB-Net-Standard driver to execute it. Should I define this as a function in the database first, and then call it using the 'Cursor' endpoints?
Note: I build up the 'data' part dynamically in the real code.
LET data = [
{
'parent': { 'from_uuid': '<parent-uuid>' },
'child': { 'to_uuid': '<child1-uuid>' }
},
{
'parent': { 'from_uuid': '<parent-uuid>' },
'child': { 'to_uuid': '<child2-uuid>' }
}
]
FOR rel in data
LET parentId = FIRST(
FOR n IN nodes
FILTER n.Properties.uuid == rel.parent.from_uuid
LIMIT 1
RETURN n._id
)
LET childId = FIRST(
FOR n IN nodes
FILTER n.Properties.uuid == rel.child.to_uuid
LIMIT 1
RETURN n._id
)
FILTER parentId != null AND childId != null
INSERT { _from: childId, _to: parentId } INTO edges
return NEW
Thanks
You can directly run your AQL query using the Cursor API, no need to create a separate user function (unless you find it useful).
The approach you've outlined should work fine, the documentation is here but it is a bit light on how to run a query with parameters ("bindvars"): https://github.com/Actify-Inc/arangodb-net-standard#run-an-aql-query
I created and ran a unit test to show how you might do this. The AQL part of that amounts to the following lines:
Here's the full test I created (
_adb
is an instance ofArangoDBClient
):As an alternative you could consider writing an AQL query to fetch the documents you need based on the UUID, then separately make a batch request using the Document API to add Edge documents (nothing prevents you from adding edge documents via the Document API despite its name). This approach using two requests could end up being faster than the single query approach, YMMV.
Or you could use the Graph API. I don't think there's a batch method using the Graph API, so you'd need to make an individual call to
PostEdgeAsync
for each edge you wish to create.The best approach might also depend on factors such as how many documents/edges you plan on adding at a time and whether you need transactional behaviour.