Update two documents in different collections in ArangoDB?

Viewed 35

I have two collections: users and tweets. I would like to know the AQL query that allows me to insert a new tweet and at the same time update the total tweets from a user.

In the users collection there is a document like: { _key:"user1", total_tweets: 0 }

In the tweets collection there are multiple "tweet" documents like: { _key:"tweet1", from_user:"users/user1" }

How can I insert a new tweet and at the same time update the total_tweets in the user?

2 Answers

If I understand correctly, you are expecting a solution that uses the trigger functionality of SQL database.

However, ArangoDB does not offer trigger functionality. So, to update the number of total_tweets in users collection after a new tweet is inserted, there are two solutions.

  1. Combine the "insert tweet query" and "update tweet count query" into one big query, and every time when you are inserting a tweet, execute this big query.
  2. Separate these two actions into two queries, execute them one by one in code, and use a transaction to ensure the ACID property of the database while executing multiple queries.

Once you INSERT to one collection, you can use the NEW document to get the result of that insert (grabbing _id or _key). Then you can run another INSERT/UPDATE/UPSERT/REPLACE on a different collection. You cannot modify the same collection twice.

The documentation talks about how to use OLD and NEW but doesn't seem to mention that updating multiple collections only works with INSERT, not UPSERT or REPLACE (throws an "OLD set multiple times" error).

However, if I'm understanding your question correctly, you want to update an integer (total_tweets) after inserting the tweet. This is the territory of transactions, which are handled with code, not through AQL.

Related