📜  DocumentDB SQL-Where子句

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


在本章中,我们将介绍WHERE子句,它也是可选的,例如FROM子句。它用于在获取源提供的JSON文档形式的数据时指定条件。任何JSON文档都必须将指定条件评估为“真”,才能考虑结果。如果满足给定条件,则仅以JSON文档的形式返回特定数据。我们可以使用WHERE子句来过滤记录并仅获取必要的记录。

在此示例中,我们将考虑相同的三个文档。以下是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 
}

让我们看一个使用WHERE子句的简单示例。

条款

在此查询中,在WHERE子句中指定了(WHERE f.id =“ WakefieldFamily”)条件。

SELECT * 
FROM f  
WHERE f.id = "WakefieldFamily"

执行上述查询后,它将返回WakefieldFamily的完整JSON文档,如以下输出所示。

[ 
   { 
      "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, 
      "_rid": "Ic8LAJFujgECAAAAAAAAAA==", 
      "_ts": 1450541623, 
      "_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgECAAAAAAAAAA==/", 
      "_etag": "\"00000500-0000-0000-0000-567582370000\"", 
      "_attachments": "attachments/" 
   } 
]