将 VALUE 关键字与文档数据库中的其他 SELECT 元素组合
Combine VALUE keyword with other SELECT Elements in Document DB
我尝试使用 VALUE 关键字结合其他特定字段来拼合 JSON 文档,但没有成功。
给定一个 Document DB 文档:
{
"id": "Tenant-Test",
"name": "Test",
"timeZone": "Eastern Standard Time",
"section1": {
"section1Key1": "section 1 value 1",
"section1Key2": "section 1 value 2",
...
},
"section2": {
"section2Key1": "section 2 value 1"
}
}
我想获取以下形状的数据子集:
{
"id": "Tenant-Test",
"name": "Test",
"timeZone": "Eastern Standard Time",
"section1Key1": "section 1 value 1",
"section1Key2": "section 1 value 2"
}
理论上我可以用
查询这个
SELECT c.id, c.name, c.timeZone, VALUE c.section1 FROM c
'VALUE' 附近出现语法错误。如果我删除特定字段 c.id、c.name、c.timeZone,那么我可以展平 c.section1.
是否可以执行此转换?
您可以使用 UDF 执行此操作:
function transform(o) {
output = {}
output.id = o.id
output.name = o.name
output.timeZone = o.timeZone
output.section1Key1 = o.section1.section1Key1
output.section1Key2 = o.section1.section1Key2
return output
}
您可以将硬编码的部分投影替换为无论多宽或多深都会变平的循环。
然后在这样的查询中使用 UDF:
SELECT VALUE udf.transform(c) FROM collection c
请注意,如果您将上面示例中的关键字 VALUE
省略,则会取消在每个文档之前添加的 $1 前缀。
Aravind 的替代解决方案如下:
SELECT c.id, c.name, c.timeZone, c.section1.section1Key1 AS section1Key1, c.section1.section1Key2 AS section1Key2
FROM c
我尝试使用 VALUE 关键字结合其他特定字段来拼合 JSON 文档,但没有成功。
给定一个 Document DB 文档:
{
"id": "Tenant-Test",
"name": "Test",
"timeZone": "Eastern Standard Time",
"section1": {
"section1Key1": "section 1 value 1",
"section1Key2": "section 1 value 2",
...
},
"section2": {
"section2Key1": "section 2 value 1"
}
}
我想获取以下形状的数据子集:
{
"id": "Tenant-Test",
"name": "Test",
"timeZone": "Eastern Standard Time",
"section1Key1": "section 1 value 1",
"section1Key2": "section 1 value 2"
}
理论上我可以用
查询这个SELECT c.id, c.name, c.timeZone, VALUE c.section1 FROM c
'VALUE' 附近出现语法错误。如果我删除特定字段 c.id、c.name、c.timeZone,那么我可以展平 c.section1.
是否可以执行此转换?
您可以使用 UDF 执行此操作:
function transform(o) {
output = {}
output.id = o.id
output.name = o.name
output.timeZone = o.timeZone
output.section1Key1 = o.section1.section1Key1
output.section1Key2 = o.section1.section1Key2
return output
}
您可以将硬编码的部分投影替换为无论多宽或多深都会变平的循环。
然后在这样的查询中使用 UDF:
SELECT VALUE udf.transform(c) FROM collection c
请注意,如果您将上面示例中的关键字 VALUE
省略,则会取消在每个文档之前添加的 $1 前缀。
Aravind 的替代解决方案如下:
SELECT c.id, c.name, c.timeZone, c.section1.section1Key1 AS section1Key1, c.section1.section1Key2 AS section1Key2
FROM c