Documentdb Sql 简明教程

DocumentDB SQL - Select Clause

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

The Azure portal has a Query Explorer that lets us run any SQL query against our DocumentDB database. We will use the Query Explorer to demonstrate the many different capabilities and features of the query language starting with the simplest possible query.

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

Step 1 − Open the Azure Portal, and in the database blade, click the Query Explorer blade.

query explorer blade

请记住,查询在集合范围内运行,所以查询浏览器允许我们在该下拉菜单中选择集合。我们将让它设置为我们的包含三个文档的 Families 集合。让我们在该示例中考虑三个文档。

Remember that queries run within the scope of a collection, and so the Query Explorer lets us choose the collection in this dropdown. We will leave it set to our Families collection that contains the three documents. Let’s consider these three documents in this example.

以下是 AndersenFamily 文档。

Following is the AndersenFamily document.

{
   "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 文档。

Following is the SmithFamily document.

{
   "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 文档。

Following is the WakefieldFamily document.

{
   "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,它简单地从集合检索所有文档。尽管简单,但它仍然与关系数据库中的等效查询有很大的不同。

The Query Explorer opens up with this simple query SELECT * FROM c, which simply retrieves all documents from the collection. Although it is simple, it’s still quite different than the equivalent query in a relational database.

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

Step 2 − In relational databases, SELECT * means return all columns while in DocumentDB. It means that you want each document in your result to be returned exactly as it’s stored in the database.

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

But when you select specific properties and expressions instead of simply issuing a SELECT *, then you are projecting a new shape that you want for each document in the result.

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

Step 3 − Click ‘Run’ to execute query and open the Results blade.

open results blade

可以看到检索到 WakefieldFamily、SmithFamily 和 AndersonFamily。

As can be seen the WakefieldFamily, the SmithFamily, and the AndersonFamily are retrieved.

以下三份文档是 SELECT * FROM c 查询结果检索到的。

Following are the three documents which are retrieved as a result of the SELECT * FROM c query.

[
   {
      "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/"
   }
]

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

However, these results also include the system-generated properties that are all prefixed with the underscore character.