简体   繁体   English

ArangoDB-如何获得两个集合之间的关系

[英]ArangoDB - How to get relations between 2 collections

I have 2 vertex collections: 我有2个顶点集合:

  • users
  • articles

and 1 edge collection: 和1个边缘集合:

  • userfollow (relation between user following other users) userfollow (跟随其他用户的用户之间的关系)

The problem is when user follow other users and the followed users wrote some articles, how to get the articles based on the user following? 问题是当用户关注其他用户并且关注的用户写了一些文章时,如何根据用户关注获取文章?

You can query the data with AQL's native graph traversal in Foxx using db._query() . 您可以使用db._query()在Foxx中使用AQL的本机图形遍历查询数据。

Sample data 样本数据

users: 用户:

{ "_key": "john-s", "gender": "m", "name": "John Smith" }

{ "_key": "jane.doe", "gender": "f", "name": "Jane Doe",
  "article_ids": [
    "salad-every-day",
    "great-aql-queries"
  ]
}

articles: 文章:

{
  "_key": "great-aql-queries",
  "title": "How to write great AQL queries"
},
{
  "_key": "salad-every-day",
  "title": "Delicious salads for every day"
}

userfollow: 用户关注:

{ "_from": "users/john-s", "_to": "users/jane.doe" }

Query 询问

Starting at follower John , we can use AQL traversal to get all users he is following. 从关注者John开始,我们可以使用AQL遍历来获取他关注的所有用户。 Here, there is only Jane being followed: 在这里,只有Jane被关注:

FOR v IN OUTBOUND "users/john-s" userfollow
    RETURN v

The document keys of the articles Jane wrote are stored in the Jane user document itself, as an array of strings (you could also model this with edges of course). Jane撰写的文章的文档密钥以字符串数组的形式存储在Jane用户文档本身中(当然,您也可以使用边进行建模)。 We can use DOCUMENT() to fetch the articles and return them: 我们可以使用DOCUMENT()来获取文章并返回它们:

FOR v IN OUTBOUND "users/john-s" userfollow
    RETURN DOCUMENT("articles", v.article_ids)

We could also return who John is following (Jane), remove the article_ids attribute for each user and merge in the full article documents: 我们还可以返回John的关注者(Jane),删除每个用户的article_ids属性,然后合并到完整的文章文档中:

FOR v IN OUTBOUND "users/john-s" userfollow
    RETURN MERGE(UNSET(v, "article_ids"), {
        articles: DOCUMENT("articles", v.article_ids)
    })

The result looks like this: 结果看起来像这样:

[
  {
    "_id": "users/jane.doe",
    "_key": "jane.doe",
    "gender": "f",
    "name": "Jane Doe",
    "articles": [
      {
        "_key": "salad-every-day",
        "_id": "articles/salad-every-day",
        "title": "Delicious salads for every day"
      },
      {
        "_key": "great-aql-queries",
        "_id": "articles/great-aql-queries",
        "title": "How to write great AQL queries"
      }
    ]
  }
]

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

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