首页 > 解决方案 > 在 gremlin 中加入查询,组导致一对多关系

问题描述

我必须输入顶点国家和机场,每个国家都有多个带有“hasAirport”标签的机场的优势

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

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"
      }
    ]
  }
 ]

标签: javagremlintinkerpopamazon-neptune

解决方案


您需要使用project

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

推荐阅读