我有2个顶点集合:
users
articles
和1个边缘集合:
userfollow
(跟随其他用户的用户之间的关系)问题是当用户关注其他用户并且关注的用户写了一些文章时,如何根据用户获取文章?
答案 0 :(得分:3)
您可以使用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"
}
userfollow:
{ "_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"
}
]
}
]