简体   繁体   English

更新 ArangoDB 中不同 collections 中的两个文档?

[英]Update two documents in different collections in ArangoDB?

I have two collections: users and tweets .我有两个 collections: userstweets 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.我想知道允许我插入新推文并同时更新用户推文总数的 AQL 查询。

In the users collection there is a document like: { _key:"user1", total_tweets: 0 }users集合中有一个文档,如: { _key:"user1", total_tweets: 0 }

In the tweets collection there are multiple "tweet" documents like: { _key:"tweet1", from_user:"users/user1" }tweets集合中有多个“tweet”文档,例如: { _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?如何插入新推文并同时更新用户的 total_tweets?

Once you INSERT to one collection, you can use the NEW document to get the result of that insert (grabbing _id or _key ). INSERT到一个集合后,您可以使用NEW文档来获取该插入的结果(获取_id_key )。 Then you can run another INSERT/UPDATE/UPSERT/REPLACE on a different collection.然后,您可以在不同的集合上运行另一个INSERT/UPDATE/UPSERT/REPLACE 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).文档讨论了如何使用OLDNEW ,但似乎没有提到更新多个 collections 仅适用于INSERT ,而不适用于UPSERTREPLACE (引发"OLD set multiple times"错误)。

However, if I'm understanding your question correctly, you want to update an integer ( total_tweets ) after inserting the tweet.但是,如果我正确理解您的问题,您想在插入推文后更新 integer ( total_tweets )。 This is the territory of transactions , which are handled with code, not through AQL.这是事务的领域,它是用代码处理的,而不是通过 AQL。

If I understand correctly, you are expecting a solution that uses the trigger functionality of SQL database.如果我理解正确,您期望使用 SQL 数据库的触发功能的解决方案。

However, ArangoDB does not offer trigger functionality.但是,ArangoDB 不提供触发器功能。 So, to update the number of total_tweets in users collection after a new tweet is inserted, there are two solutions.因此,要在插入新推文后更新用户集合中的 total_tweets 数量,有两种解决方案。

  1. Combine the and into one big query, and every time when you are inserting a tweet, execute this big query.将 and 组合成一个大查询,每次插入推文时,执行这个大查询。
  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.将这两个动作分成两个查询,在代码中一一执行,并在执行多个查询时使用事务来保证数据库的ACID属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM