如何加入一个数组中的元素,该数组也是另一个数组的一部分,以在 couchbase 中获得最佳性能?

How to join an element which is in an array which is also an part of another array for best performance in couchbase?

以下是组织的示例文档

{
"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.Each addressAssociation 包含一个 addressId 并对应于这个 addressId 地址存储在地址文档中。

现在我必须从两个文档中获取 organizationRole 名称、organizationRole id、城市、zip。 为了在 couchbase 中获得最佳性能,处理这种情况的最佳方法应该是什么?

我正在考虑使用连接,但无法针对这种情况提出准确的查询。 我尝试了以下查询,但它不起作用。

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 是数字,在地址中,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";

查看https://blog.couchbase.com/ansi-join-support-n1ql/