Documentdb Sql 简明教程

DocumentDB SQL - From Clause

在本章中,我们将介绍 FROM 子句,其与常规 SQL 中的标准 FROM 子句的作用完全不同。

查询总是运行在特定集合的上下文中,不能在集合中的文档之间进行联接,这让我们疑惑为什么需要 FROM 子句。事实上,我们不需要,但是如果不包含它,那么我们就不会查询集合中的文档。

此子句的目的是指定查询必须在其上运行的数据源。通常整个集合都是源,但也可以指定集合的子集。 FROM <from_specification> 子句是可选的,除非在查询中对源进行了筛选或投影。

让我们再次看同一个例子。以下是 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
}
wakefield family

在上面的查询中,“ SELECT * FROM c ”表示整个 Families 集合是用来枚举的源。

Sub-documents

也可以将源缩小到更小的子集。当我们只想检索每个文档中的子树时,子根就可以成为源,如下面的示例所示。

sub documents

当我们运行以下查询时 −

SELECT * FROM Families.parents

将检索以下子文档。

[
   [
      {
         "familyName": "Wakefield",
         "givenName": "Robin"
      },

      {
         "familyName": "Miller",
         "givenName": "Ben"
      }
   ],

   [
      {
         "familyName": "Smith",
         "givenName": "James"
      },

      {
         "familyName": "Curtis",
         "givenName": "Helen"
      }
   ],

   [
      {
         "firstName": "Thomas",
         "relationship": "father"
      },

      {
         "firstName": "Mary Kay",
         "relationship": "mother"
      }
   ]
]

根据此查询结果,我们可以看到仅检索了 parents 子文档。