在像这样的Azure Documentdb文档中
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 1,
"pets": [
{ "givenName": "Goofy" },
{ "givenName": "Shadow" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 8
}
],
"address": { "state": "NY", "county": "Manhattan", "city": "NY" },
"isRegistered": false
};
如何查询给名字的宠物是“高飞”的孩子?
看起来以下语法无效
Select * from root r
WHERE r.children.pets.givenName="Goofy"
相反,我需要做
Select * from root r
WHERE r.children[0].pets[0].givenName="Goofy"
并没有真正搜索数组。
关于如何处理这类查询的任何建议?
答案 0 :(得分:42)
你应该利用DocumentDB的JOIN
子句,它在RDBM中的运行方式与JOIN
略有不同(因为DocumentDB处理无架构文档的非规范化数据模型)。
简单地说,您可以将DocumentDB的JOIN
视为自连接,可用于在嵌套的JSON对象之间形成交叉产品。
在查询名字为“高飞”的儿童的情况下,您可以尝试:
SELECT
f.id AS familyName,
c AS child,
p.givenName AS petName
FROM Families f
JOIN c IN f.children
JOIN p IN c.pets
WHERE p.givenName = "Goofy"
返回:
[{
familyName: WakefieldFamily,
child: {
familyName: Merriam,
givenName: Jesse,
gender: female,
grade: 1,
pets: [{
givenName: Goofy
}, {
givenName: Shadow
}]
},
petName: Goofy
}]
参考:http://azure.microsoft.com/en-us/documentation/articles/documentdb-sql-query/
修改强>
您还可以使用ARRAY_CONTAINS
功能,如下所示:
SELECT food.id, food.description, food.tags
FROM food
WHERE food.id = "09052" or ARRAY_CONTAINS(food.tags.name, "blueberries")
答案 1 :(得分:4)
自2014年得到解答以来,我认为ARRAY_CONTAINS函数已经更改。我必须使用以下内容使其起作用。
SELECT * FROM c
WHERE ARRAY_CONTAINS(c.Samples, {TimeBasis:"5MIN_AV", "Value":"5.105"},true)
样本是我的JSON数组,它包含具有许多属性的对象,包括上面的两个。