我有 2 个顶点集合:
users
articles
和 1 个边缘集合:
userfollow
(用户关注其他用户之间的关系)
问题是当用户关注其他用户并且被关注的用户写了一些文章时,如何根据用户关注来获取文章?
您可以使用db._query()在 Foxx 中使用 AQL 的本机图形遍历查询数据。
用户:
{ "_key": "john-s", "gender": "m", "name": "John Smith" }
{ "_key": "jane.doe", "gender": "f", "name": "Jane Doe",
"article_ids": [
"salad-every-day",
"great-aql-queries"
]
}
文章:
{
"_key": "great-aql-queries",
"title": "How to write great AQL queries"
},
{
"_key": "salad-every-day",
"title": "Delicious salads for every day"
}
用户关注:
{ "_from": "users/john-s", "_to": "users/jane.doe" }
从关注者John开始,我们可以使用AQL 遍历来获取他关注的所有用户。在这里,只有简被跟踪:
FOR v IN OUTBOUND "users/john-s" userfollow
RETURN v
Jane 撰写的文章的文档键存储在 Jane 用户文档本身中,作为字符串数组(当然,您也可以使用边对其进行建模)。我们可以使用DOCUMENT()来获取文章并返回它们:
FOR v IN OUTBOUND "users/john-s" userfollow
RETURN DOCUMENT("articles", v.article_ids)
我们还可以返回 John 正在关注的人 (Jane),删除article_ids
每个用户的属性并合并到完整的文章文档中:
FOR v IN OUTBOUND "users/john-s" userfollow
RETURN MERGE(UNSET(v, "article_ids"), {
articles: DOCUMENT("articles", v.article_ids)
})
结果如下所示:
[
{
"_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"
}
]
}
]