Cosmos DB SQL API > 筛选出具有特定值的子数组对象
Cosmos DB SQL API > Filter out child array objects with specific values
我有一个包含 属性 的文档,它是一个对象数组。我想编写一个查询来过滤掉子数组中的对象。我认为 array_ contains 可以解决问题,但它似乎没有过滤子数组。
查询
SELECT Families.id, Families.parents
FROM Families
WHERE ARRAY_CONTAINS(Families.parents, { givenName: "Ben", familyName: "Wakefield" })
结果
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
}
]
}
]
想要的结果
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Ben"
}
]
}
]
Cosmos DB 可以实现吗SQLAPI?
谢谢,
斯科特
您似乎使用官方示例数据作为 Cosmos DB 容器中的项目。
{
"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" },
"creationDate": 1431620462,
"isRegistered": false
}
您的 SQL 将获得父数组中包含 { givenName: "Robin", familyName: "Wakefield" }
和整个父数组 return 的项目。这就是您没有得到想要的结果的原因。
SELECT Families.id, Families.parents
FROM Families
WHERE ARRAY_CONTAINS(Families.parents, { givenName: "Robin", familyName: "Wakefield" })
您可以使用Array()
函数构造一个数组来添加子查询的结果来做到这一点。像这样:
SELECT Families.id, ARRAY(SELECT p.familyName,p.givenName FROM p in Families.parents WHERE p.familyName ='Wakefield' and p.givenName = 'Robin') AS parents
FROM Families
结果:
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Robin"
}
]
}
]
我有一个包含 属性 的文档,它是一个对象数组。我想编写一个查询来过滤掉子数组中的对象。我认为 array_ contains 可以解决问题,但它似乎没有过滤子数组。
查询
SELECT Families.id, Families.parents
FROM Families
WHERE ARRAY_CONTAINS(Families.parents, { givenName: "Ben", familyName: "Wakefield" })
结果
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
}
]
}
]
想要的结果
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Ben"
}
]
}
]
Cosmos DB 可以实现吗SQLAPI?
谢谢,
斯科特
您似乎使用官方示例数据作为 Cosmos DB 容器中的项目。
{
"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" },
"creationDate": 1431620462,
"isRegistered": false
}
您的 SQL 将获得父数组中包含 { givenName: "Robin", familyName: "Wakefield" }
和整个父数组 return 的项目。这就是您没有得到想要的结果的原因。
SELECT Families.id, Families.parents
FROM Families
WHERE ARRAY_CONTAINS(Families.parents, { givenName: "Robin", familyName: "Wakefield" })
您可以使用Array()
函数构造一个数组来添加子查询的结果来做到这一点。像这样:
SELECT Families.id, ARRAY(SELECT p.familyName,p.givenName FROM p in Families.parents WHERE p.familyName ='Wakefield' and p.givenName = 'Robin') AS parents
FROM Families
结果:
[
{
"id": "WakefieldFamily",
"parents": [
{
"familyName": "Wakefield",
"givenName": "Robin"
}
]
}
]