【发布时间】:2016-08-03 10:10:08
【问题描述】:
我有 2 个顶点集合:
usersarticles
和 1 个边缘集合:
-
userfollow(用户关注其他用户的关系)
问题是当用户关注其他用户时,被关注的用户写了一些文章,如何根据用户关注获取文章?
【问题讨论】:
-
你如何存储用户和他/她写的文章之间的关系?是否有一组文档句柄(文章 ID),或者是否有另一个边缘集合来存储这些关系?
我有 2 个顶点集合:
usersarticles和 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 traversal 获取他关注的所有用户。在这里,只有 Jane 被跟踪:
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"
}
]
}
]
【讨论】: