couchbase - 如何获取特定格式的 n1ql 查询响应?

标签 couchbase sql++

当我执行下面的查询时

SELECT r.name AS organizationRoleName, r.organizationRoleId, a.city, a.zip,a.address1
FROM `contact` AS c
UNNEST c.organizationRoles AS r
UNNEST r.addressAssociations AS aa
jOIN `optima_contact` AS a
ON aa.addressId = TO_NUMBER(a.addressId) 
WHERE  c.type = "organization" AND a.type = "address" and a.city="Plaridel";

我得到的回复如下

[
   {
 "address1": "Ground Floor Waltermart Center-Plaridel, Cagayan Valley Road, Barrio Banga 1, Plaridel",
"city": "Plaridel",
"organizationRoleId": 903,
"organizationRoleName": "SUN - WALTERMART PLARIDEL",
"zip": "3004"
},
{
"address1": "Ground Floor Waltermart Center-Plaridel Cagayan Valley Road Barrio Banga 1 Plaridel",
"city": "Plaridel",
"organizationRoleId": 1001,
"organizationRoleName": "FRS1 Store",
"zip": "3004"
  }
]

有没有办法以下面的格式而不是上面的格式获取响应?

[
{
"organizationRoleId": 903,
"organizationRoleName": "SUN - WALTERMART PLARIDEL",
"storeAddress": {
                "address1": "Ground Floor Waltermart Center-Plaridel, Cagayan Valley Road, Barrio Banga 1, Plaridel",
                "city": "Plaridel",
                "zipCode": "3004"
            }
},
{
"organizationRoleId": 1001,
"organizationRoleName": "FRS1 Store",
"storeAddress": {
                "address1": "Ground Floor Waltermart Center-Plaridel Cagayan Valley Road Barri Banga 
                             1 Plaridel",
                "city": "Plaridel",
                "zipCode": "3004"
            }
  }
  ]

如何获得上述类型的响应?

最佳答案

您可以使用 {city, zip, address1} 和 Alias AS storeAddress 构造对象

SELECT r.name AS organizationRoleName, r.organizationRoleId,
       {a.city, a.zip,a.address1} AS storeAddress
FROM `contact` AS c
UNNEST c.organizationRoles AS r
UNNEST r.addressAssociations AS aa
jOIN `optima_contact` AS a
ON aa.addressId = TO_NUMBER(a.addressId) 
WHERE  c.type = "organization" AND a.type = "address" and a.city="Plaridel";

关于couchbase - 如何获取特定格式的 n1ql 查询响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59028890/

相关文章:

go - Couchbase GoLang 客户端找不到存储桶

couchbase - 将 Couchbase DB 的 n1ql 查询输出中的数字转换为字符串

couchbase - 如何在 Couchbase 中查询两个日期之间的数据

couchbase - n1ql 主扫描性能

尝试使用以下查询删除条目时,Couchbase 查询抛出错误 3000

java - Spring-data-couchbase - 运行更新查询

java - 在 Couchbase Java SDK 2.x 中检查和设置?

redis - 如何在 Ubuntu 上为 redis 集群运行 YCSB

cassandra - 我不确定哪种 NoSQL 适合我的场景

database - Couchbase 5.1 N1QL OR 运算符不使用索引