Documentdb Sql 简明教程

DocumentDB SQL - Joins

在关系型数据库中,Join 子句用于合并数据库中来自两个或更多表的记录,并且在设计归一化架构时,非常需要连接跨表。由于 DocumentDB 处理无架构文档的非规范化数据模型,因此 DocumentDB SQL 中的 JOIN 是“selfjoin”的逻辑等效。

让我们考虑一下前一个示例中的三个文档。

以下是 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
}

我们来看一个示例,以了解 JOIN 子句的工作原理。

sql join

以下是将根连接到子文档的查询。

SELECT f.id
FROM Families f
JOIN c IN f.children

执行上述查询时,将会产生以下输出。

[
   {
      "id": "WakefieldFamily"
   },

   {
      "id": "WakefieldFamily"
   },

   {
      "id": "SmithFamily"
   },

   {
      "id": "SmithFamily"
   },

   {
      "id": "AndersenFamily"
   }
]

在上述示例中,连接在文档根和子根之间,在两个 JSON 对象之间生成笛卡尔积。以下是需要注意的几个要点 -

  1. 在 FROM 子句中,JOIN 子句是一个迭代器。

  2. 前两个文档 WakefieldFamily 和 SmithFamily 包含两个子文档,因此结果集包含笛卡尔积,为每个子文档生成一个独立的对象。

  3. 第三个文档 AndersenFamily 只包含一个子文档,因此只有一个对应于此文档的对象。

让我们来看相同的示例,但这一次,我们还检索子名称,以便更好地理解 JOIN 子句。

sql joins

以下是将根连接到子文档的查询。

SELECT
   f.id AS familyName,
   c.givenName AS childGivenName,
   c.firstName AS childFirstName
FROM Families f
JOIN c IN f.children

执行以上查询后,将产生以下输出。

[
   {
      "familyName": "WakefieldFamily",
      "childGivenName": "Jesse"
   },

   {
      "familyName": "WakefieldFamily",
      "childGivenName": "Lisa"
   },

   {
      "familyName": "SmithFamily",
      "childGivenName": "Michelle"
   },

   {
      "familyName": "SmithFamily",
      "childGivenName": "John"
   },

   {
      "familyName": "AndersenFamily",
      "childFirstName": "Henriette Thaulow"
   }
]