- DocumentDB SQL 教程
- DocumentDB SQL - 主页
- DocumentDB SQL - 概述
- DocumentDB SQL - 选择子句
- DocumentDB SQL - From 子句
- DocumentDB SQL -Where 子句
- DocumentDB SQL - 运算符
- DocumentDB - Between 关键字
- DocumentDB SQL - In 关键字
- DocumentDB SQL - 值关键字
- 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 - From 子句
在本章中,我们将介绍 FROM 子句,它的工作方式与常规 SQL 中的标准 FROM 子句完全不同。
查询总是在特定集合的上下文中运行,并且不能跨集合内的文档进行连接,这让我们想知道为什么需要 FROM 子句。事实上,我们没有,但如果我们不包含它,那么我们就不会查询集合中的文档。
该子句的目的是指定查询必须操作的数据源。通常,整个集合是源,但也可以指定集合的子集。FROM <from_specation> 子句是可选的,除非稍后在查询中过滤或投影源。
让我们再看一下同一个例子。以下是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
}
以下是史密斯家族的文件。
{
"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
}
在上面的查询中,“ SELECT * FROM c ”表示整个 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"
}
]
]
作为此查询的结果,我们可以看到仅检索了父子文档。