使用OrientDB 2.2.16并在文档数据库中导入以下数据:
{
"teams": [
{
"name": "McLaren F1 Team",
"nationality": "british",
"headquarters": {
"city": "Woking",
"country": "England"
},
"drivers": [
{
"name": "Fernando Alonso",
"nationality": "Spanish",
"yearOfBirth": "1980"
},
{
"name": "Jenson Button",
"nationality": "British",
"yearOfBirth": "1980"
}
]
},
{
"name": "Scuderia Ferrari",
"nationality": "italian",
"headquarters": {
"city": "Maranello",
"country": "Italy"
},
"drivers": [
{
"name": "Sebastian Vettel",
"nationality": "German",
"yearOfBirth": "1987"
},
{
"name": "Kimi Raikkonen",
"nationality": "Finnish",
"yearOfBirth": "1979"
}
]
}
]
}
使用展开,我想查找返回所有驱动程序名称的查询。确切地说,结果必须是一个文档列表,其中每个文档都包含一个名为" name"的属性中的驱动程序名称。
我的(不工作)尝试:
SELECT drivers.name FROM Teams unwind drivers
它几乎返回我的预期,但名称放在名为" drivers"的属性下。
SELECT drivers.name AS name FROM Teams unwind drivers
完全失败,根本没有放松。
SELECT drivers.name AS name FROM Teams unwind name
这很有效,但实际上它是一种错误,因为别名适用于驱动程序,而不是名称,以及展开的原因。
答案 0 :(得分:3)
关于如何详细阐述查询的一些背景知识:
Teams
数据从存储在您的情况下,在此步骤中您有两条记录:
查询1:v 2.2中drivers.name的默认别名为drivers
(在v 3.0中,这将更改,默认别名为drivers.name
)
| drivers |
+-----------------------------------------+
| ["Fernando Alonso", "Jenson Button" ] |
| ["Sebastian Vettel", "Kimi Raikkonen" ] |
查询2和3:在这种情况下,别名是name
,您是明确定义的
| name |
+-----------------------------------------+
| ["Fernando Alonso", "Jenson Button" ] |
| ["Sebastian Vettel", "Kimi Raikkonen" ] |
查询1按预期展开drivers
,但别名仍为drivers
查询2尝试展开drivers
,但显然没有找到它,这就是它失败的原因
查询3按预期展开name
作为结论:这是预期的行为