本文介绍了通过边缘将父顶点和子节点分组为master/sub json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我正在使用天蓝色的宇宙数据库.

First of i am using azure cosmos db.

一个人works_for多个办公室.每个Office可以是IsMaster,也可以不是.如果它是IsMaster,则可以在另一个Office处具有master_of边缘.每个works_for边都有一个人的AccessLevel属性.

A person works_for multiple Offices. Each Office can be IsMaster or not. If it is a IsMaster it can have master_of edge to another Office. Each works_for edge has AccessLevel property for a person.

逻辑:给定一个人员名称,获取IsMaster ='true'的该人员works_for的所有局.然后返回取自works_for的所有IsMasterAccessLevel以及每个IsMaster拥有通过master_of边缘以及相应的AccessLevelworks_for

Logic: Given a Person name, get all Offices that person works_for that are IsMaster = 'true'. Then return all IsMaster with AccessLevel taken from works_for and corresponding SubOffices that each IsMaster has through master_of edge and corresponding AccessLevel through works_for

我的最终目标是在给定人名的情况下在下面生成一个JSON对象:

My end goal is to produce a JSON object below given a person name:

[
  {
    "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"值:

Update: I had one of my edges missing a 'AccessLevel' property that .by() was referencing. Without that property query was failing. Using coalesce() I set it to a "foobar" value before anything else:

outE("works_for").as("e").coalesce(values('AccessLevel'), property('AccessLevel','foo')).select('e').

推荐答案

您的查询(创建图形的查询)在CosmosDB中是否真的有效?如果其他人想跟进,下面是创建没有嵌套g.V()的图的查询:

Does your query (the one, that creates the graph) really work in CosmosDB? If others want to follow along, here's the query that creates the graph without nested g.V()'s:

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())

这篇关于通过边缘将父顶点和子节点分组为master/sub json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 17:35