Documentdb Sql 简明教程

DocumentDB SQL - Select Clause

Azure 门户有一个查询浏览器,它让我们针对我们的 DocumentDB 数据库运行任何 SQL 查询。我们将使用查询浏览器演示查询语言的许多不同功能和特性,从最简单的查询开始。

Step 1 - 打开 Azure 门户并在数据库边栏中单击查询浏览器边栏。

query explorer blade

请记住,查询在集合范围内运行,所以查询浏览器允许我们在该下拉菜单中选择集合。我们将让它设置为我们的包含三个文档的 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,它简单地从集合检索所有文档。尽管简单,但它仍然与关系数据库中的等效查询有很大的不同。

Step 2 - 在关系数据库中,SELECT * 表示返回所有列,而在 DocumentDB 中,这意味着您希望结果中的每个文档完全按其在数据库中存储的方式返回。

但是,当您选择特定属性和表达式而不是仅仅发出 SELECT * 时,那么您就会针对结果中的每个文档规划您想要的新形状。

Step 3 - 单击“运行”执行查询并打开结果边栏。

open results blade

可以看到检索到 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/"
   }
]

但是,这些结果还包括系统生成的所有以下划线字符为前缀的属性。