DocumentDB SQL - Select 子句
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 }
以下是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 }
查询资源管理器会打开这个简单的查询 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/" } ]
但是,这些结果还包括系统生成的所有以下划线字符前缀的属性。