在 gremlin 中加入查询,将结果分组为一对多关系

Join query in gremlin, group results in a one to many relationship

我必须输入顶点国家和机场的类型,每个国家都有一个到多个机场的边,标签为 "hasAirport"

我正在尝试一个连接查询,它将 return 国家与他们拥有的机场分组。

g.V().hasLabel("Country").as("country").out('hasAirport').as("airport").select("country", "airport").by(__.unfold().valueMap(true).fold()).toList()

如果我的图表中只有一个国家(例如美国)和两个机场,则查询结果如下所示。

[   {
    "country": [
      {
        "name": "United States",
        "code": "USA",      "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 1234,
        "label": "Airport",
        "name": "San Francisco International Airport",      "code": "SFO"
      }
    ]   },   {
    "country": [
      {
        "name": "United States",
        "code": "USA",      "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 2345,
        "label": "Airport",
        "name": "Austin Bergstrom International Airport",       "code": "AUS"
      }
    ]   }  ]

有没有办法像下面这样在一个数组中合并多个机场

[
  {
    "country": [
      {
        "name": "United States",
        "code": "USA",
        "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 1234,
        "label": "Airport",
        "name": "San Francisco International Airport",
        "code": "SFO"
      },
      {
        "id": 2345,
        "label": "Airport",
        "name": "Austin Bergstrom International Airport",
        "code": "AUS"
      }
    ]
  }
 ]

您需要使用project:

g.V().hasLabel("Country")
.project("country","airport")
.by(valueMap(true))
.by(out('hasAirport').valueMap(true).fold())