简体   繁体   English

OrientDB在两个相关的顶点上选择

[英]OrientDB Select on two related vertex

I have a scenario as shown below 我有一个如下所示的情况 在此处输入图片说明 ,

I want to query the database so I get the following result, 我想查询数据库,所以得到以下结果,

User    Resource      Permissions

Edi     Plan A        [view]

Where 哪里

resource.name = 'Plan A' and user.name = 'Edi'

my query for above is 我对上面的查询是

SELECT name, 
       out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name, 
       set(out('hasARole').out('hasA').name) as permission 
FROM user 
WHERE name = 'Edi'

It should display 它应该显示

User    Resource      Permissions

Adrian   Plan A        [view,edit, delete]

if I change it to, 如果我改成

Where 哪里

resource.name = 'Plan A' and user.name = 'Adrian'

my query for above is 我对上面的查询是

SELECT name, 
       out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name, 
       set(out('hasARole').out('hasA').name) as permission 
FROM user 
WHERE name = 'Adrian'

Now above queries work as long as the users don't have another role on another type of resource. 现在,只要用户在另一种类型的资源上没有其他角色,上述查询就可以工作。 eg if Edi had Admin role on let's say a resource type of Workspace then the query gives me back all the permissions that an Admin would have , instead of just view as he only has view permission on Plan A 例如,如果Edi工作空间的资源类型上具有管理员角色,那么该查询将给我发回一个管理员将拥有的所有权限,而不仅仅是查看,因为他仅对计划A具有查看权限

I have used the following graph for my answer. 我已使用以下图形作为答案。 Note that I have corrected some incositencies with your original edges. 请注意,我已经用您的原始边缘纠正了一些不便之处。

调整图

I see a number of possible queries for this problem. 我看到了许多可能的查询此问题。 I am a bit confused why you would want to return the User and Resource in the query, as you probably already have these records due to the fact you use them to create the query. 我有些困惑,为什么要在查询中返回用户和资源,因为由于使用它们来创建查询,您可能已经拥有了这些记录。 You can't 'nest' the full records in the results either (unless you JSON them). 您也不能“嵌套”结果中的完整记录(除非您对它们进行JSON处理)。 Further to this, querying on the name field, and returning only the name field seem a little nonsensical to me - but maybe you have done so to simplify the question. 除此之外,对名称字段进行查询,仅返回名称字段对我来说似乎有点荒谬-但也许您这样做是为了简化问题。 Regardless, the following queries will get you on your way to your desired results. 无论如何,以下查询都会助您一臂之力。

My first idea is to run a query to get all of the Roles related to a Resource. 我的第一个想法是运行查询以获取与资源相关的所有角色。 We then run a query over these results to filter for the Roles that include the User. 然后,我们对这些结果运行查询,以筛选出包含用户的角色。 This looks like the following; 如下所示;

select from (
    select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"
) where in('hasARole') contains first((select from User where name = "Edi"))

This query correctly returns just the Viewer record for both Edi and Adrian. 此查询正确地只返回Edi和Adrian的Viewer记录。

My second idea is to run 1 query for the Roles related to a Resource (similar to above), and another for the Roles related to a User, and then find the intersect. 我的第二个想法是对与资源相关的角色运行1个查询(类似于上面的查询),对与用户相关的角色运行另一个查询,然后找到相交处。 This looks like the following, and gives the same results as the query above; 如下所示,并提供与上述查询相同的结果;

select expand(intersect) from (
    select intersect($resource_roles, $user_roles)
    let
    $resource_roles = (select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"),
    $user_roles = (select expand(out('hasARole')) from User where name = "Edi")
)

Now if you really do want the User, Resource and Permissions all in the 1 result, you can use the following, or a variant of; 现在,如果您确实希望将“用户”,“资源”和“权限”全部包含在1个结果中,则可以使用以下内容或其中的一种:

select first($user).name as User, first($resource).name as Resource, intersect(resource_roles,     user_roles).name as Permissions from (
    select $resource.out('isOfType').in('ofType') as resource_roles, $user.out('hasARole') as user_roles
    let
    $resource = (select from Resource where name = "Plan A"),
    $user = (select from User where name = "Edi")
)

or 要么

select first($user).name as User, first($resource).name as Resource, intersect($resource_roles, $user_roles).name as Permissions
let
$resource = (select from Resource where name = "Plan A"),
$resource_roles = (select expand(out('isOfType').in('ofType')) from $parent.$resource),
$user = (select from User where name = "Edi"),
$user_roles = (select expand(out('hasARole')) from $parent.$user)

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

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