- 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 - 选择子句
Azure 门户有一个查询资源管理器,可让我们针对 DocumentDB 数据库运行任何 SQL 查询。我们将使用查询资源管理器从最简单的查询开始,演示查询语言的许多不同功能和特性。
步骤 1 - 打开 Azure 门户,然后在数据库边栏选项卡中单击“查询资源管理器”边栏选项卡。
请记住,查询在集合范围内运行,因此查询资源管理器允许我们在此下拉列表中选择集合。我们将把它保留在包含这三个文档的 Families 集合中。让我们在这个例子中考虑这三个文档。
以下是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,它只是从集合中检索所有文档。尽管它很简单,但它与关系数据库中的等效查询仍然有很大不同。
步骤 2 - 在关系数据库中,SELECT * 表示返回 DocumentDB 中的所有列。这意味着您希望结果中的每个文档都按照数据库中存储的方式准确返回。
但是,当您选择特定的属性和表达式而不是简单地发出 SELECT * 时,您就会为结果中的每个文档投影一个您想要的新形状。
步骤 3 - 单击“运行”执行查询并打开“结果”边栏选项卡。
可以看到,WakefieldFamily、SmithFamily 和 AndersonFamily 均被检索。
以下是作为SELECT * FROM c查询结果检索到的三个文档。
[
{
"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,
"_rid": "Ic8LAJFujgECAAAAAAAAAA==",
"_ts": 1450541623,
"_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgECAAAAAAAAAA==/",
"_etag": "\"00000500-0000-0000-0000-567582370000\"",
"_attachments": "attachments/"
},
{
"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,
"_rid": "Ic8LAJFujgEDAAAAAAAAAA==",
"_ts": 1450541623,
"_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgEDAAAAAAAAAA==/",
"_etag": "\"00000600-0000-0000-0000-567582370000\"",
"_attachments": "attachments/"
},
{
"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,
"_rid": "Ic8LAJFujgEEAAAAAAAAAA==",
"_ts": 1450541624,
"_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgEEAAAAAAAAAA==/",
"_etag": "\"00000700-0000-0000-0000-567582380000\"",
"_attachments": "attachments/"
}
]
但是,这些结果还包括系统生成的所有以下划线字符前缀的属性。