我的文档具有如下所示的嵌套结构
{
"docType": "account",
"accounts": [
{
"id": "123123",
"name": "abcdCompany",
"owner": "abcdCompany corporation",
"email": "[email protected]",
"projects": [
{
"id": "1",
"name": "abcdCompany asset management",
"owner": "assetMgmt",
"email": "[email protected]"
},
{
"id": "2",
"name": "abcdCompany alert notification",
"owner": "alertNotification",
"email": "[email protected]"
}
]
}
]
}
我们如何获得具有“所有者”的项目:“ alertNotification”
我不确定您的想法(您可能需要check out this SO question了解UNNEST
与UNNEST
的详细信息),但是如果您只想选择所有者=='alertNotification的嵌套项目对象',您可以使用ANY/SATISFIES
。您在数组中有一个数组,因此需要两次ANY/SATISFIES
:
UNNEST
将会返回:
UNNEST
[如果您不想进行不必要的操作,而只想返回包含其帐户的项目拥有“ alertNotification”的“所有者”的项目的任何文档,则可以使用SELECT prj.*
FROM moviegame b
UNNEST b.accounts acct
UNNEST acct.projects prj
WHERE prj.owner == 'alertNotification';
(再次嵌套,因为数组中有一个数组):
[
{
"email": "[email protected]",
"id": "2",
"name": "abcdCompany alert notification",
"owner": "alertNotification"
}
]
这将返回示例中的整个文档(如下),但其中不包含其他没有拥有owner =='alertNotification'项目的文档:
ANY/SATISFIES