教程:使用 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?

通过 REST API 和 SDK查询演练(它对现有示例数据集运行查询),可在 Azure 门户中使用数据资源管理器运行查询。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 查询返回其 ID 字段匹配 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

下一查询返回家族中所有 ID 匹配 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 multiple-regionally.