通过边将父节点和子节点分组到 master/sub json 数组中
Group Parent Vertex and Children through Edges into master/sub json array
首先我使用的是 azure cosmos db。
一个人works_for
多个办公室。每个 Office 可以是 IsMaster
也可以不是。如果它是 IsMaster
它可以 master_of
到另一个办公室。每个 works_for
边有 AccessLevel
属性 一个人。
逻辑:给定一个人名,得到那个人 works_for
的所有办公室 IsMaster
= 'true'。然后 return 所有 IsMaster
和 AccessLevel
取自 works_for
和每个 IsMaster
通过 master_of
边缘和相应 AccessLevel
的相应子办公室通过 works_for
我的最终目标是在给定人名的情况下生成一个 JSON 对象:
[
{
"Master": {
"Name": "Seattle",
"AccessLevel": "Admin"
},
"Subs": [
{
"Name": "Portland",
"AccessLevel": "NonAdmin"
},
{
"Name": "Vancouver",
"AccessLevel": "Admin"
}
]
},
{
"Master": {
"Name": "New York",
"AccessLevel": "NonAdmin"
},
"Subs": [
{
"Name": "Boston",
"AccessLevel": "NonAdmin"
},
{
"Name": "Orlando",
"AccessLevel": "Admin"
}
]
}
]
g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1')
.addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2')
.addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3')
.addV('Office').property('Name','New York').property('IsMaster','true').as('oe1')
.addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2')
.addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3')
.addV('Person').property('Name','Zodiak').as('p')
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Seattle'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Portland'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Vancouver'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'New York'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Boston'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Orlando'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Portland'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Vancouver'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Boston'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Orlando'))
更新:我的一条边缺少 .by() 引用的 'AccessLevel'
属性。没有那个 属性 查询失败。使用 coalesce()
我先将其设置为 "foobar"
值:
outE("works_for").as("e").coalesce(values('AccessLevel'), property('AccessLevel','foo')).select('e').
您的查询(创建图表的查询)在 CosmosDB 中真的有效吗?如果其他人想要跟随,这里是创建没有嵌套 g.V()
的图形的查询:
g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1').
addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2').
addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3').
addV('Office').property('Name','New York').property('IsMaster','true').as('oe1').
addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2').
addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3').
addV('Person').property('Name','Zodiak').as('p').
addE('works_for').property('AccessLevel','Admin').to('ow1').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('ow2').select('p').
addE('works_for').property('AccessLevel','Admin').to('ow3').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('oe1').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('oe2').select('p').
addE('works_for').property('AccessLevel','Admin').to('oe3').select('ow1').
addE('master_of').to('ow2').select('ow1').
addE('master_of').to('ow3').select('oe1').
addE('master_of').to('oe2').select('oe1').
addE('master_of').to('oe3')
以及产生所需结果的查询:
g.V().has("Person","Name","Zodiak").as("p").
outE("works_for").as("e").
inV().has("IsMaster","true").
project("Master","Subs").
by(project("Name","AccessLevel").
by("Name").
by(select("e").by("AccessLevel"))).
by(out("master_of").as("o").
inE("works_for").where(outV().as("p")).
project("Name","AccessLevel").
by(select("o").by("Name")).
by("AccessLevel").fold())
首先我使用的是 azure cosmos db。
一个人works_for
多个办公室。每个 Office 可以是 IsMaster
也可以不是。如果它是 IsMaster
它可以 master_of
到另一个办公室。每个 works_for
边有 AccessLevel
属性 一个人。
逻辑:给定一个人名,得到那个人 works_for
的所有办公室 IsMaster
= 'true'。然后 return 所有 IsMaster
和 AccessLevel
取自 works_for
和每个 IsMaster
通过 master_of
边缘和相应 AccessLevel
的相应子办公室通过 works_for
我的最终目标是在给定人名的情况下生成一个 JSON 对象:
[
{
"Master": {
"Name": "Seattle",
"AccessLevel": "Admin"
},
"Subs": [
{
"Name": "Portland",
"AccessLevel": "NonAdmin"
},
{
"Name": "Vancouver",
"AccessLevel": "Admin"
}
]
},
{
"Master": {
"Name": "New York",
"AccessLevel": "NonAdmin"
},
"Subs": [
{
"Name": "Boston",
"AccessLevel": "NonAdmin"
},
{
"Name": "Orlando",
"AccessLevel": "Admin"
}
]
}
]
g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1')
.addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2')
.addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3')
.addV('Office').property('Name','New York').property('IsMaster','true').as('oe1')
.addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2')
.addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3')
.addV('Person').property('Name','Zodiak').as('p')
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Seattle'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Portland'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Vancouver'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'New York'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Boston'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Orlando'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Portland'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Vancouver'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Boston'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Orlando'))
更新:我的一条边缺少 .by() 引用的 'AccessLevel'
属性。没有那个 属性 查询失败。使用 coalesce()
我先将其设置为 "foobar"
值:
outE("works_for").as("e").coalesce(values('AccessLevel'), property('AccessLevel','foo')).select('e').
您的查询(创建图表的查询)在 CosmosDB 中真的有效吗?如果其他人想要跟随,这里是创建没有嵌套 g.V()
的图形的查询:
g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1').
addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2').
addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3').
addV('Office').property('Name','New York').property('IsMaster','true').as('oe1').
addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2').
addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3').
addV('Person').property('Name','Zodiak').as('p').
addE('works_for').property('AccessLevel','Admin').to('ow1').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('ow2').select('p').
addE('works_for').property('AccessLevel','Admin').to('ow3').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('oe1').select('p').
addE('works_for').property('AccessLevel','NonAdmin').to('oe2').select('p').
addE('works_for').property('AccessLevel','Admin').to('oe3').select('ow1').
addE('master_of').to('ow2').select('ow1').
addE('master_of').to('ow3').select('oe1').
addE('master_of').to('oe2').select('oe1').
addE('master_of').to('oe3')
以及产生所需结果的查询:
g.V().has("Person","Name","Zodiak").as("p").
outE("works_for").as("e").
inV().has("IsMaster","true").
project("Master","Subs").
by(project("Name","AccessLevel").
by("Name").
by(select("e").by("AccessLevel"))).
by(out("master_of").as("o").
inE("works_for").where(outV().as("p")).
project("Name","AccessLevel").
by(select("o").by("Name")).
by("AccessLevel").fold())