简体   繁体   English

Cosmos DB 键值对查询

[英]Cosmos DB query on key-value pairs

I have a large collection of json documents whose structure is in the form:我收集了大量 json 份文件,其结构形式如下:

{
    "id": "00000000-0000-0000-0000-000000001122",
    "typeId": 0,
    "projectId": "p001",
    "properties": [
        {
            "id": "a6fdd321-562c-4a40-97c7-4a34c097033d",
            "name": "projectName",
            "value": "contoso",
        },
        {
            "id": "d3b5d3b6-66de-47b5-894b-cdecfc8afc40",
            "name": "status",
            "value": "open",
        },
        .....{etc}
    ]
}

There may be a lot of properties in the collection, all identified by the value of name.集合中可能有很多属性,都是通过name的值来标识的。 The fields in properties are pretty consistent -- there may be some variability, but they will all have the fields that I care about.属性中的字段非常一致——可能会有一些变化,但它们都会有我关心的字段。 There's an Id, some labels, etc有一个 Id,一些标签等

I'm wanting to combine these with some other data in PowerBI using the projectId to create some very valuable reports.我想使用 projectId 将这些与 PowerBI 中的其他一些数据结合起来,以创建一些非常有价值的报告。

I think what I want to do it 'normalize' this data into a table, like:我想我想做的是将这些数据“规范化”到一个表中,例如:

ProjectId项目编号 projectName项目名 status地位 openDate打开日期 closeDate关闭日期 manager经理
p001 p001 contoso contoso open打开 20200101 20200101 me
etc ETC

Where I'm at...我在什么地方...

I can go:我可以 go:

SELECT c["value"] AS ProjectName 
FROM c in t.Properties
WHERE c["name"] = "projectName"

... this will give me each projectName ...这将给我每个项目名称

I can do that a heap of times to get the 'values' (status, openDate, manager, etc)我可以多次这样做以获得“值”(状态、openDate、经理等)

If I want to combine them together then I would need to combine all those sub-queries together with 'id'.如果我想将它们组合在一起,那么我需要将所有这些子查询与“id”组合在一起。 But 'id' in not in the scope of the SELECT, so how do I get it??但是 'id' 不在 SELECT 的 scope 中,那我怎么获取呢? If I were to do this, it sounds like something that would be very expensive (RU's) to execute.如果我要这样做,这听起来像是执行起来非常昂贵 (RU) 的事情。

I think I'm overcomplicating this, but I cant quite get my head around the Cosmos syntax.我认为我过于复杂了,但我无法完全理解 Cosmos 语法。

Help??帮助??

You can achieve it with JOINS and the WHERE expressions although the scheme is not ideal for querying and you should consider changing it.您可以使用JOINSWHERE表达式来实现它,尽管该方案对于查询来说并不理想,您应该考虑更改它。

SELECT 
    c['projectId'], --c.projectId also works, but value is a reserved keyword
    n['value'] AS projectName,
    s['value'] AS status
FROM c
JOIN n IN c.properties
JOIN s IN c.properties
WHERE n['name'] = 'projectName' AND s['name'] = 'status'
--note all filtered properties must appear exactly once for it to work properly

Edit;编辑; new query that solves the potential issue that filtered properties must appear exactly once.解决过滤属性必须恰好出现一次的潜在问题的新查询。

SELECT 
    c['projectId'],
    ARRAY(
        SELECT VALUE n['value']
        FROM n IN c.properties
        WHERE  n['name'] = 'projectName'
    )[0] AS projectName,
    ARRAY(
        SELECT VALUE n['value']
        FROM n IN c.properties
        WHERE  n['name'] = 'status'
    )[0] AS status
FROM c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM