在Azure DocumentDB/Azure CosmosDB中选择字典的键和值

在Azure DocumentDB/Azure CosmosDB中选择字典的键和值,azure,azure-cosmosdb,nosql,Azure,Azure Cosmosdb,Nosql,考虑存储在DocumentDB中的这两个示例文档 文件1 文件2 我想为每个文档选择Properties对象中的所有键和所有值。 比如: SELECT c.JobId, c.JobType, c.Properties.<Keys> AS Keys, c.Properties.<Values> AS Values FROM c 我能够在DocumentDB中使用UDF解决我的问题。因为KeyNames是一个数组。Self-join正在返回密钥 所以这个查询 SELECT

考虑存储在DocumentDB中的这两个示例文档

文件1

文件2

我想为每个文档选择Properties对象中的所有键和所有值。 比如:

SELECT 
c.JobId,
c.JobType,
c.Properties.<Keys> AS Keys,
c.Properties.<Values> AS Values
FROM c

我能够在DocumentDB中使用UDF解决我的问题。因为KeyNames是一个数组。Self-join正在返回密钥

所以这个查询

SELECT 
c.JobId,
c.JobType,
Key,
udf.GetValueUsingKey(c.Properties, Key) AS Value
FROM collection AS c
JOIN Key in c.KeyNames
返回我想要的结果

您可以使用DocumentDB中提供的脚本资源管理器来定义UDF。 为了我的目的,我使用了:

function GetValueUsingKey(Properties, Key) { 
   var result = Properties[Key];
   return JSON.stringify(result);
}
希望这有助于:

[
{
"JobId": "04e63d1d-2af1-42af-a349-810f55817602",
"JobType": 3,
"Key1": "Value1"
}

{
"JobId": "04e63d1d-2af1-42af-a349-810f55817602",
"JobType": 3,
"Key2": "Value2"
}

{
"JobId": "04e63d1d-2af1-42af-a349-810f55817603",
"JobType": 4,
"Key3": "Value3"
}

{
"JobId": "04e63d1d-2af1-42af-a349-810f55817603",
"JobType": 4,
"Key4": "Value4"
 }
]
SELECT 
c.JobId,
c.JobType,
Key,
udf.GetValueUsingKey(c.Properties, Key) AS Value
FROM collection AS c
JOIN Key in c.KeyNames
function GetValueUsingKey(Properties, Key) { 
   var result = Properties[Key];
   return JSON.stringify(result);
}