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

问题描述 投票:0回答:1

下面是组织的示例文档

{
"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对应地址存储在地址文档中。

现在我必须从两个文档中获取organizationRole名称,organizationRole id,city,zip。在沙发床中获得最佳性能的最佳方法是什么?

我正在考虑使用连接,但无法针对这种情况提出确切的查询。我已经尝试过以下查询,但无法正常工作。

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";
couchbase n1ql
1个回答
0
投票

您的方向正确。

在addressAssociations中,addressId是数字,在地址中addressId是字符串。字符串和数字不相同,也没有隐式类型转换。您必须修复数据或使用TOSTRING(),TONUMBER()等进行显式类型转换...

N1QL字段名称也区分大小写,您使用addressID vs 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/

© www.soinside.com 2019 - 2024. All rights reserved.