与Couchbase的动态场值查询

发布于 2025-02-09 19:42:43 字数 1480 浏览 2 评论 0原文

使用Couchbase Bucket的以下结构,如何查询嵌套DOC是否具有动态字段名称?

在这里,我想返回在hyderabad中使用帐户的客户文档,

我试图以这种方式查询但无法成功。

select * from bucket where accounts.$.city = 'Hyderabad'

我期望通过电子邮件,但无法成功。

Couchbase Docs

[
{
  "type": "customer",
  "customer_id": <UUID4>,
  "user_type": "owner",
  "first_name": "",
  "last_name": "",
  "email": "[email protected]",
  "password": "",
  "phone_number": 11111,
  "accounts": {
    <account_id which is UUID4>: {
      "amount": "500",
      "city": "Hyderabad"
    }
  }
},
{
    "type": "customer",
    "customer_id": <UUID4>,
    "user_type": "employee",
    "first_name": "",
    "last_name": "",
    "email": "[email protected]",
    "password": "",
    "phone_number": 33333,
    "accounts": {
      <account_id which is UUID4>: {
        "amount": "500",
        "city": "Chennai"
      }
    }
  }
]

在Couchbase中是否有办法以这种方式获取?

With the below structure of Couchbase bucket, how do I query if the nested doc has dynamic field name?

Here, I would like to return the customer docs who have account in Hyderabad

I tried to query this way but couldn't succeed.

select * from bucket where accounts.$.city = 'Hyderabad'

I was expecting to return the customer doc with email [email protected] but couldn't succeed.

Couchbase docs

[
{
  "type": "customer",
  "customer_id": <UUID4>,
  "user_type": "owner",
  "first_name": "",
  "last_name": "",
  "email": "[email protected]",
  "password": "",
  "phone_number": 11111,
  "accounts": {
    <account_id which is UUID4>: {
      "amount": "500",
      "city": "Hyderabad"
    }
  }
},
{
    "type": "customer",
    "customer_id": <UUID4>,
    "user_type": "employee",
    "first_name": "",
    "last_name": "",
    "email": "[email protected]",
    "password": "",
    "phone_number": 33333,
    "accounts": {
      <account_id which is UUID4>: {
        "amount": "500",
        "city": "Chennai"
      }
    }
  }
]

Is there a way in Couchbase to fetch in this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2025-02-16 19:42:43
SELECT b.*
FROM bucket AS b
WHERE ANY v IN OBJECT_VALUES(b.accounts) SATISFIES v.city = 'Hyderabad' END;

或者

SELECT b.*
FROM bucket AS b
WHERE ANY n:v IN b.accounts SATISFIES v.city = 'Hyderabad' END;
SELECT b.*
FROM bucket AS b
WHERE ANY v IN OBJECT_VALUES(b.accounts) SATISFIES v.city = 'Hyderabad' END;

OR

SELECT b.*
FROM bucket AS b
WHERE ANY n:v IN b.accounts SATISFIES v.city = 'Hyderabad' END;
深海少女心 2025-02-16 19:42:43

VSR的答案为问题提供了整洁的解决方案。还请记住,您将需要提供索引,以便最佳地利用Couchbase查询服务。我猜(根据您的文档示例),您的存储桶中包含多种文档类型。考虑到这一点,这是一个示例索引创建语句:

create index idxTypeCustomer on bucket(type) where type = 'customer';

现在您可以将其用作WHERE子句的一部分:

SELECT b.*
FROM bucket AS b
WHERE type = 'customer'
  AND ANY v IN OBJECT_VALUES(b.accounts) SATISFIES v.city = 'Hyderabad' END;

The answer from VSR provides a neat solution to the problem. Remember also that you will want to provide an index in order to leverage the Couchbase query services optimally. I'm guessing (based on your doc examples) that you will have multiple document types included in your bucket. With that in mind, here is an example index create statement:

create index idxTypeCustomer on bucket(type) where type = 'customer';

Now you can use it as part of your WHERE clause:

SELECT b.*
FROM bucket AS b
WHERE type = 'customer'
  AND ANY v IN OBJECT_VALUES(b.accounts) SATISFIES v.city = 'Hyderabad' END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文