- 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 執行迭代的查詢擁有不同的輸出陣列。因此,將每個家庭中的所有家長都新增到單個數組中。
廣告