📜  DocumentDB SQL-值关键字

📅  最后修改于: 2020-11-28 13:35:20             🧑  作者: Mango


当您知道只返回一个值时,那么VALUE关键字可以避免创建完整对象的开销,从而有助于产生更精简的结果集。 VALUE关键字提供了一种返回JSON值的方法。

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

值关键字

以下是使用VALUE关键字进行的查询。

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

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

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

在另一个示例中,让我们考虑前面示例中的三个文档。

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