在MySql中,我们使用两个模型用户和友谊创建简单的用户友谊数据库。
模型友谊包含键:
在ArangoDB中制作相同内容的正确方法是什么, 可以有一个"友谊"带有附加键值的边缘文档? 是否可以执行更新边缘文档? 此外,作为结果执行请求后,这些值是否可能显示?
答案 0 :(得分:3)
举个例子。
首先创建一个"用户"收藏和友谊"集合。
arangosh [_system]> db._create("users");
[ArangoCollection 33746098, "users" (type document, status loaded)]
arangosh [_system]> db._createEdgeCollection("friendship");
[ArangoCollection 33877170, "friendship" (type edge, status loaded)]
创建两个用户
arangosh [_system]> db.users.save({_key: "you"});
{
"_id" : "users/you",
"_rev" : "34598066",
"_key" : "you"
}
arangosh [_system]> db.users.save({_key: "me"});
{
"_id" : "users/me",
"_rev" : "34794674",
"_key" : "me"
}
和他们之间的关系
arangosh [_system]> db.friendship.save("users/me", "users/you", {});
{
"_id" : "friendship/35515570",
"_rev" : "35515570",
"_key" : "35515570"
}
检查用户
arangosh [_system]> db._query("FOR u IN users RETURN u").toArray()
[
{
"_id" : "users/me",
"_rev" : "34794674",
"_key" : "me"
},
{
"_id" : "users/you",
"_rev" : "34598066",
"_key" : "you"
}
]
和他们的邻居
arangosh [_system]> db._query("FOR u IN users RETURN { user: u, friends: NEIGHBORS(users, friendship, u, 'outbound') }").toArray()
[
{
"user" : {
"_id" : "users/me",
"_key" : "me",
"_rev" : "34794674"
},
"friends" : [
"users/you"
]
},
{
"user" : {
"_id" : "users/you",
"_key" : "you",
"_rev" : "34598066"
},
"friends" : [ ]
}
]
添加"阻止"
arangosh [_system]> db._query("FOR f IN friendship FILTER f._from == 'users/me' and f._to == 'users/you' UPDATE f WITH { block: true } IN friendship RETURN { before: OLD, after: NEW }").toArray()
[
{
"before" : {
"_id" : "friendship/35515570",
"_rev" : "35515570",
"_key" : "35515570",
"_from" : "users/me",
"_to" : "users/you"
},
"after" : {
"block" : true,
"_id" : "friendship/35515570",
"_rev" : "63892658",
"_key" : "35515570",
"_from" : "users/me",
"_to" : "users/you"
}
}
]
返回将返回整个文档。您也可以只返回块属性
arangosh [_system]> db._query("FOR f IN friendship FILTER f._from == 'users/me' and f._to == 'users/you' UPDATE f WITH { block: false } IN friendship RETURN { before: OLD.block, after: NEW.block }").toArray()
[
{
"before" : true,
"after" : false
}
]