如何在 Couchbase 中为同一数组的 2 个不同键编写 REGEXP_CONTAINS

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

我的 Couchbase 数据库中有 3 个文档:-

{
    "appId": "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182",
    "assignedTo": "Sweta",
    "attributes": [
       {
         "activity": "Supplier Migration Request"
       },
       {
         "comments": "great work"
       },
       {
         "assignedTo": "Sweta"
       }
    ],
   "status": "InProgress",
   "task": {
     "details": {
        "subject": "RE: Complexpromo-vnagara1-7708-cz",
      },
     "taskType": "mail"
   },
   "taskId": "142af79d-123f-4990-a9aa-5e3e4ea7723c",
   "type": "task"
},
{
   "appId": "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182",
   "assignedTo": "Santhosh",
   "attributes": [
      {
        "activity": "Supplier Migration Request"
      },
      {
        "comments": "work"
      },
      {
        "assignedTo": "Santhosh"
      }
    ],
   "status": "InProgress",
   "task": {
     "details": {
        "subject": "RE: Complexpromo-vnagara1-7708-cz",
      },
     "taskType": "mail"
   },
   "taskId": "242af79d-123f-4990-a9aa-5e3e4ea7723c",
   "type": "task"
},
{
   "appId": "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182",
   "assignedTo": "Santhosh",
   "attributes": [
      {
        "activity": "Supplier Request"
      },
      {
        "comments": "your work is not completed"
      },
      {
        "assignedTo": "Santhosh"
      }
    ],
   "status": "New",
   "task": {
     "details": {
        "subject": "RE: task-detail-7708-cz",
      },
     "taskType": "mail"
   },
   "taskId": "342af79d-123f-4990-a9aa-5e3e4ea7723c",
   "type": "task"
}

我正在寻找一个查询,它给我的数据在属性的“评论”中有关键字“工作”,在属性部分的“活动”中有关键字“迁移”。

我试过写一个查询:-

select wf.* from workflow wf UNNEST attributes attr where wf.appId="f1606a90-c96f-4db9-8e5f-9f0b6d2d2182" AND wf.type="task" and REGEXP_CONTAINS(attr.activity, "Migration+.*") and REGEXP_CONTAINS(attr.comments, "work+.*") and REGEXP_CONTAINS(wf.status, "InProgress+.*") limit 5;

预期结果:-

{
    "appId": "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182",
    "assignedTo": "Sweta",
    "attributes": [
       {
         "activity": "Supplier Migration Request"
       },
       {
         "comments": "great work"
       },
       {
         "assignedTo": "Sweta"
       }
    ],
   "status": "InProgress",
   "task": {
     "details": {
        "subject": "RE: Complexpromo-vnagara1-7708-cz",
      },
     "taskType": "mail"
   },
   "taskId": "142af79d-123f-4990-a9aa-5e3e4ea7723c",
   "type": "task"
},
{
   "appId": "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182",
   "assignedTo": "Santhosh",
   "attributes": [
      {
        "activity": "Supplier Migration Request"
      },
      {
        "comments": "work"
      },
      {
        "assignedTo": "Santhosh"
      }
    ],
   "status": "InProgress",
   "task": {
     "details": {
        "subject": "RE: Complexpromo-vnagara1-7708-cz",
      },
     "taskType": "mail"
   },
   "taskId": "242af79d-123f-4990-a9aa-5e3e4ea7723c",
   "type": "task"
}

由于前 2 份文件在关键字“活动”中同时包含“迁移”,在关键字“评论”中包含“工作”,因此答案应该是前 2 份文件。 其中第三个文档在关键字“activity”中不是“Migration”,因此即使它在关键字“comments”中包含“work”也不会被选中。

知道如何获得预期的结果吗?或者如何即兴创作我当前的查询?任何线索都会有所帮助。

couchbase n1ql
1个回答
0
投票
SELECT wf.*
FROM workflow wf
WHERE wf.appId = "f1606a90-c96f-4db9-8e5f-9f0b6d2d2182"
      AND wf.type = "task"
      AND ANY attr IN wf.attributes SATISFIES REGEXP_CONTAINS(attr.activity, "Migration+.*") END
      AND ANY attr IN wf.attributes SATISFIES REGEXP_CONTAINS(attr.comments, "work+.*") END
      AND REGEXP_CONTAINS(wf.status, "InProgress+.*")
LIMIT 5;

使用 UNNEST 只是 self JOIN 主要查询每个数组元素。如果您需要从 JOIN 开始投影合格的 ARRAY 元素,请使用 UNNEST。

如果要匹配 ARRAY 元素之一作为谓词和项目主文档,请使用 ANY。

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