java - 在 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"
}
]
}
]
解决方案
您需要使用project
:
g.V().hasLabel("Country")
.project("country","airport")
.by(valueMap(true))
.by(out('hasAirport').valueMap(true).fold())
推荐阅读
- javascript - 开玩笑单元测试 - 如何在 setTimeout 重复函数中通过异步函数调用
- vue.js - Vue组件计算没有反应
- python - python数据框中的平等
- python-3.x - 如何根据其他数据框的行将新列添加到数据框中?
- reactjs - 在反应中动态加载 svgs 作为组件
- spring-integration - 如何处理spring集成中的错误
- jquery - 如何根据 select onChange 事件更新 jQuery datetimepicker
- azure - 如何不为磁盘多收费用?
- react-native - React Native - 警告:可能未处理的承诺拒绝(id:0)
- .net - EPPLus [Nuget] 依赖项