教學課程:使用 SQL API 查詢 Azure Cosmos DBTutorial: Query Azure Cosmos DB by using the SQL API

Azure Cosmos DB SQL API 支援使用 SQL 來查詢文件。The Azure Cosmos DB SQL API supports querying documents using SQL. 本文提供一個範例文件及兩個範例 SQL 查詢和結果。This article provides a sample document and two sample SQL queries and results.

本文涵蓋下列工作:This article covers the following tasks:

  • 使用 SQL 來查詢資料Querying data with SQL

範例文件Sample document

本文中的 SQL 查詢使用下列範例文件。The SQL queries in this article use the following sample document.

{
  "id": "WakefieldFamily",
  "parents": [
      { "familyName": "Wakefield", "givenName": "Robin" },
      { "familyName": "Miller", "givenName": "Ben" }
  ],
  "children": [
      {
        "familyName": "Merriam", 
        "givenName": "Jesse", 
        "gender": "female", "grade": 1,
        "pets": [
            { "givenName": "Goofy" },
            { "givenName": "Shadow" }
        ]
      },
      { 
        "familyName": "Miller", 
         "givenName": "Lisa", 
         "gender": "female", 
         "grade": 8 }
  ],
  "address": { "state": "NY", "county": "Manhattan", "city": "NY" },
  "creationDate": 1431620462,
  "isRegistered": false
}

我可以在哪裡執行 SQL 查詢?Where can I run SQL queries?

您可以使用 Azure 入口網站中的 [資料總管]、透過 REST API 和 SDK,甚至是 Query Playground (在一組現有的範例資料上執行查詢),來執行查詢。You can run queries using the Data Explorer in the Azure portal, via the REST API and SDKs, and even the Query playground, which runs queries on an existing set of sample data.

如需有關 SQL 查詢的詳細資訊,請參閱:For more information about SQL queries, see:

必要條件Prerequisites

本教學課程會假設您具備 Azure Cosmos DB 帳戶和集合。This tutorial assumes you have an Azure Cosmos DB account and collection. 不符合上述其中任何一項條件嗎?Don't have any of those? 完成 5 分鐘快速入門Complete the 5-minute quickstart.

範例查詢 1Example query 1

在提供上述範例家族文件的情況下,下列 SQL 查詢會傳回識別碼欄位符合 WakefieldFamily 的文件。Given the sample family document above, following SQL query returns the documents where the id field matches WakefieldFamily. 由於它是一個 SELECT * 陳述式,因為查詢的輸出是完整的 JSON 文件:Since it's a SELECT * statement, the output of the query is the complete JSON document:

查詢Query

SELECT * 
FROM Families f 
WHERE f.id = "WakefieldFamily"

結果Results

{
  "id": "WakefieldFamily",
  "parents": [
      { "familyName": "Wakefield", "givenName": "Robin" },
      { "familyName": "Miller", "givenName": "Ben" }
  ],
  "children": [
      {
        "familyName": "Merriam", 
        "givenName": "Jesse", 
        "gender": "female", "grade": 1,
        "pets": [
            { "givenName": "Goofy" },
            { "givenName": "Shadow" }
        ]
      },
      { 
        "familyName": "Miller", 
         "givenName": "Lisa", 
         "gender": "female", 
         "grade": 8 }
  ],
  "address": { "state": "NY", "county": "Manhattan", "city": "NY" },
  "creationDate": 1431620462,
  "isRegistered": false
}

範例查詢 2Example query 2

下一個查詢會傳回家族中識別碼符合 WakefieldFamily 的小孩名字,並依年級排序。The next query returns all the given names of children in the family whose id matches WakefieldFamily ordered by their grade.

查詢Query

SELECT c.givenName 
FROM Families f 
JOIN c IN f.children 
WHERE f.id = 'WakefieldFamily'

結果Results

[ { "givenName":"Jesse" }, { "givenName":"Lisa" } ][ { "givenName": "Jesse" }, { "givenName": "Lisa" } ]

後續步驟Next steps

在本教學課程中,您已完成下列操作:In this tutorial, you've done the following:

  • 了解如何使用 SQL 來進行查詢Learned how to query using SQL

您現在可以繼續進行到下一個教學課程,以了解如何全域散發您的資料。You can now proceed to the next tutorial to learn how to distribute your data globally.