[英]Update two documents in different collections in ArangoDB?
I have two collections: users and tweets .我有两个 collections: users和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.我想知道允许我插入新推文并同时更新用户推文总数的 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).该文档讨论了如何使用OLD
和NEW
,但似乎没有提到更新多个 collections 仅适用于INSERT
,而不适用于UPSERT
或REPLACE
(引发"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 数量,有两种解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.