- DocumentDB SQL 教程
- DocumentDB SQL - 主页
- DocumentDB SQL - 概览
- DocumentDB SQL - Select 子句
- DocumentDB SQL - From 子句
- DocumentDB SQL - Where 子句
- DocumentDB SQL - 运算符
- DocumentDB - Between 关键字
- DocumentDB SQL - In 关键字
- DocumentDB SQL - Value 关键字
- DocumentDB SQL - Order By 子句
- DocumentDB SQL - 迭代
- DocumentDB SQL - 联接
- DocumentDB SQL - 别名
- DocumentDB SQL - 数组创建
- DocumentDB - 标量表达式
- DocumentDB SQL - 参数化
- DocumentDB SQL - 内置函数
- Linq 转到 SQL 翻译
- JavaScript 集成
- 用户定义函数
- 复合 SQL 查询
- DocumentDB SQL 有用资源
- DocumentDB SQL - 快速指南
- DocumentDB SQL - 有用资源
- DocumentDB SQL - 讨论
DocumentDB SQL - 迭代
在 DocumentDB SQL 中,Microsoft 添加了一个可配合 IN 关键字使用的新构造,以提供支持通过 JSON 数组进行迭代的功能。从子句提供迭代支持。
我们将再次考虑前几个示例中的三个类似文档。
以下是 AndersenFamily 文档。
{
"id": "AndersenFamily",
"lastName": "Andersen",
"parents": [
{ "firstName": "Thomas", "relationship": "father" },
{ "firstName": "Mary Kay", "relationship": "mother" }
],
"children": [
{
"firstName": "Henriette Thaulow",
"gender": "female",
"grade": 5,
"pets": [ { "givenName": "Fluffy", "type": "Rabbit" } ]
}
],
"location": { "state": "WA", "county": "King", "city": "Seattle" },
"isRegistered": true
}
以下是 SmithFamily 文档。
{
"id": "SmithFamily",
"parents": [
{ "familyName": "Smith", "givenName": "James" },
{ "familyName": "Curtis", "givenName": "Helen" }
],
"children": [
{
"givenName": "Michelle",
"gender": "female",
"grade": 1
},
{
"givenName": "John",
"gender": "male",
"grade": 7,
"pets": [
{ "givenName": "Tweetie", "type": "Bird" }
]
}
],
"location": {
"state": "NY",
"county": "Queens",
"city": "Forest Hills"
},
"isRegistered": true
}
以下是 WakefieldFamily 文档。
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 6,
"pets": [
{ "givenName": "Charlie Brown", "type": "Dog" },
{ "givenName": "Tiger", "type": "Cat" },
{ "givenName": "Princess", "type": "Cat" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 3,
"pets": [
{ "givenName": "Jake", "type": "Snake" }
]
}
],
"location": { "state": "NY", "county": "Manhattan", "city": "NY" },
"isRegistered": false
}
让我们看一个没有 IN 关键字的 from 子句中的简单示例。
以下是将返回 Families 集合中所有家长的查询。
SELECT * FROM Families.parents
执行上述查询时,将产生以下输出。
[
[
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
}
],
[
{
"familyName": "Smith",
"givenName": "James"
},
{
"familyName": "Curtis",
"givenName": "Helen"
}
],
[
{
"firstName": "Thomas",
"relationship": "father"
},
{
"firstName": "Mary Kay",
"relationship": "mother"
}
]
]
如上一个输出中所示,每个家庭的家长都显示在单独的 JSON 数组中。
让我们看看同一个示例,但是,这次我们将在 from 子句中使用 IN 关键字。
以下是包含 IN 关键字的查询。
SELECT * FROM c IN Families.parents
执行上述查询时,将产生以下输出。
[
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
},
{
"familyName": "Smith",
"givenName": "James"
},
{
"familyName": "Curtis",
"givenName": "Helen"
},
{
"firstName": "Thomas",
"relationship": "father"
},
{
"firstName": "Mary Kay",
"relationship": "mother"
}
{
"id": "WakefieldFamily",
"givenName": "Jesse",
"grade": 6
}
]
在上面的示例中,可以看到,使用迭代,对集合中的 parent 执行迭代的查询拥有不同的输出数组。因此,将每个家庭中的所有家长都添加到单个数组中。
广告