Documentdb Sql 简明教程

DocumentDB SQL - Value Keyword

当您知道您仅返回单个值时,VALUE 关键字可以通过避免创建完整对象开销来帮助生成精简的结果集。VALUE 关键字提供了一种返回 JSON 值的方法。

When you know you’re only returning a single value, then the VALUE keyword can help produce a leaner result set by avoiding the overhead of creating a full-blown object. The VALUE keyword provides a way to return JSON value.

我们来看一个简单的例子。

Let’s take a look at a simple example.

value keyword

以下是包含 VALUE 关键字的查询。

Following is the query with VALUE keyword.

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

当执行此查询时,他返回标量“Hello World,这是 DocumentDB SQL 教程”。

When this query is executed, it returns the scalar "Hello World, this is DocumentDB SQL Tutorial".

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

在另一个例子中,我们来看看上面例子中的三个文档。

In another example, let’s consider the three documents from the previous examples.

以下是 AndersenFamily 文档。

Following is the AndersenFamily document.

{
   "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 文档。

Following is the SmithFamily document.

{
   "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 文档。

Following is the WakefieldFamily document.

{
   "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
}

以下是查询。

Following is the query.

SELECT VALUE f.location
FROM Families f

当执行此查询时,他返回地址而不带位置标签。

When this query is executed, it return the returns the address without the location label.

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

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

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

如果我们现在指定相同的查询而无 VALUE 关键字,那么将会返回带有位置标签的地址。以下是查询。

If we now specify the same query without VALUE Keyword, then it will return the address with location label. Following is the query.

SELECT f.location
FROM Families f

执行此查询时,将得到以下输出。

When this query is executed, it produces the following output.

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

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

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