DocumentDB查询嵌套对象

时间:2017-01-11 20:48:20

标签: c# azure azure-cosmosdb

我在查询DocumentDB中的嵌套对象时遇到了问题。我无法控制数据的格式。让我们说一个对象在DocumentDB中是这样的:

{
    "SCHEMA_ID": {
        "PROJECT": "A",
        "MODEL": "B",
        "GUID":"A GUID"
    },
    "STATE": {
        "Active": "True"
    },
    "OBJECTS": {
        "OBJECT": [
            {
                "ATTR_VALS": {
                    "NAME": "Header",
                    "ID": "0",
                    "VALUE": [
                        {
                            "NAME": "JobId",
                            "VAL": "1011656"
                        },
                        {
                            "NAM": "Region",
                            "VAL": "West Coast"
                        }
                    ]
                }
            },
            {
                "ATTR_VALS": {
                    "NAME": "SampleData",
                    "ID": "0",
                    "VALUE": [
                        {
                            "NAME": "Height",
                            "VAL": "5"
                        },
                        {
                            "NAM": "Length",
                            "VAL": "3"
                        }
                    ]
                }
            }
        ]
    }
}

我想找到所有具有' ATTR_VALS' =' SampleData'并且那些物品的高度为' = 5

我到目前为止:

SELECT test.GUID
FROM test
join OBJECTS in test.OBJECTS
join OBJECT in OBJECTS
join ATTR_VALS in OBJECT
join VALUE in ATTR_VALS
WHERE ATTR_VALS.NAME = 'SampleData' AND VALUE.NAME='Height' AND   VALUE.VAL='5'

但这不起作用,并且不会返回任何结果。谢谢!

1 个答案:

答案 0 :(得分:2)

查询必须是:

SELECT test.SCHEMA_ID.GUID
FROM test
join OBJ in test.OBJECTS.OBJECT
join VAL in OBJ.ATTR_VALS["VALUE"]
WHERE OBJ.ATTR_VALS.NAME = "SampleData" AND VAL.NAME='Height' AND VAL.VAL='5'

我改变了几件事:

  • 必须对数组执行JOIN,而不是对象。可以使用“。”运算符
  • 扩展对象
  • VALUE是一个特殊关键字,必须转义
  • 投影条款中的小错误,缺少SCHEMA_ID