Documentdb-sql-value-keyword

提供:Dev Guides
移動先:案内検索

DocumentDB SQL-値キーワード

単一の値のみを返すことがわかっている場合、VALUEキーワードは、本格的なオブジェクトを作成するオーバーヘッドを回避することで、よりスリムな結果セットを生成するのに役立ちます。 VALUEキーワードは、JSON値を返す方法を提供します。

簡単な例を見てみましょう。

Value KeyWord

以下は、VALUEキーワードを使用したクエリです。

SELECT VALUE "Hello World, this is DocumentDB SQL Tutorial"

このクエリが実行されると、スカラー「Hello World、これはDocumentDB SQLチュートリアル」を返します。

[
   "Hello World, this is DocumentDB SQL Tutorial"
]

別の例では、前の例の3つのドキュメントを考えてみましょう。

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

以下はクエリです。

SELECT VALUE f.location
FROM Families f

このクエリが実行されると、位置ラベルなしで住所を返します。

[
   {
      "state": "NY",
      "county": "Manhattan",
      "city": "NY"
   },

   {
      "state": "NY",
      "county": "Queens",
      "city": "Forest Hills"
   },

   {
      "state": "WA",
      "county": "King",
      "city": "Seattle"
   }
]

VALUEキーワードなしで同じクエリを指定すると、ロケーションラベル付きの住所が返されます。 以下はクエリです。

SELECT f.location
FROM Families f

このクエリを実行すると、次の出力が生成されます。

[
   {
      "location": {
         "state": "NY",
         "county": "Manhattan",
         "city": "NY"
      }
   },

   {
      "location": {
         "state": "NY",
         "county": "Queens",
         "city": "Forest Hills"
      }
   },

   {
      "location": {
         "state": "WA",
         "county": "King",
         "city": "Seattle"
      }
   }
]