以下是组织的示例文档
{
"org": {
"id": "org_2_1084",
"organizationId": 1084,
"organizationName": "ABC",
"organizationRoles": [
{
"addressAssociations": [
{
"activeDate": "2019-08-03T18:52:00.857Z",
"addressAssocTypeId": -2,
"addressId": 100,
"ownershipStatus": 1,
"srvAddressStatus": 1
},
{
"activeDate": "2019-08-03T18:52:00.857Z",
"addressAssocTypeId": -2,
"addressId": 105,
"ownershipStatus": 1,
"srvAddressStatus": 1
}
],
"name": "NLUZ",
"organizationRoleId": 893,
"roleSpecId": -104,
"statusId": 1,
"statusLastChangedDate": "2019-08-04T13:14:44.616Z"
},
{
"addressAssociations": [
{
"activeDate": "2019-08-03T18:52:00.857Z",
"addressAssocTypeId": -2,
"addressId": 582,
"ownershipStatus": 1,
"srvAddressStatus": 1
},
{
"activeDate": "2019-08-03T18:52:00.857Z",
"addressAssocTypeId": -2,
"addressId": 603,
"ownershipStatus": 1,
"srvAddressStatus": 1
}
],
"name": "TXR",
"organizationRoleId": 894,
"partyRoleAssocs": [
{
"partyRoleAssocId": "512"
}
],
"roleSpecId": -103,
"statusId": 1,
"statusLastChangedDate": "2019-08-04T13:14:44.616Z"
},
} 下面是地址的示例文档
{
"address": {
"address1": "string",
"address2": "string",
"addressId": "1531",
"changeWho": "string",
"city": "string",
"fxGeocode": "string",
"houseNumber": "string",
"id": "1531",
"isActive": true,
"postalCode": "string",
"state": "string",
"streetName": "string",
"tenantId": "2",
"type": "address",
"zip": "string"
}
}
一个组织中有多个organizationRoles,一个organizationRole中有多个addressAssociations。每个addressAssociation都包含一个addressId并对应于这个addressId 地址存储在地址文档中。
现在我必须从两个文档中获取组织角色名称、组织角色 ID、城市、邮政编码。 为了在 couchbase 中获得最佳性能,解决这种情况的最佳方法应该是什么?
我正在考虑使用 join,但无法针对这种情况提出精确的查询。 我已尝试以下查询,但它不起作用。
select *
from 'contact' As A UNNEST 'contact'.organizationRoles as Roles
UNNEST Roles.addressAssociations address
Join 'contact' As B
on address.addressID=B.addressID
where A.type="organization" and B.type="address";
最佳答案
你的方向是正确的。
在addressAssociations中,addressId是数字,在address中,addressId是字符串。字符串和数字不相同,并且没有隐式类型转换。您必须修复数据或使用 TOSTRING()、TONUMBER() 等进行显式类型转换...
此外,N1QL 字段名称区分大小写,您使用addressID 与addressId(在文档中)的查询
SELECT r.name AS organizationRoleName, r.organizationRoleId, a.city, a.zip
FROM contact AS c
UNNEST c.organizationRoles AS r
UNNEST r.addressAssociations AS aa
jOIN contact AS a
ON aa.addressId = a.addressId
WHERE c.type = "organization" AND a.type = "address";
CREATE INDEX ix1 ON contact(addressId, city, zip) WHERE type = "address";
关于couchbase - 如何加入一个数组中的元素,该数组也是另一个数组的一部分,以便在 couchbase 中获得最佳性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840734/