📜  DocumentDB SQL-选择子句

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


Azure门户具有查询资源管理器,该查询资源管理器使我们可以对DocumentDB数据库运行任何SQL查询。从最简单的查询开始,我们将使用查询资源管理器来演示查询语言的许多不同功能。

步骤1-打开Azure门户,然后在数据库刀片中,单击查询资源管理器刀片。

查询资源管理器刀片

请记住,查询在集合的范围内运行,因此查询浏览器使我们可以在此下拉列表中选择集合。我们将其设置为包含三个文档的Families集合。让我们在此示例中考虑这三个文档。

以下是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 * FROM c打开,该查询仅从集合中检索所有文档。尽管很简单,但它与关系数据库中的等效查询还是有很大不同的。

步骤2-在关系数据库中,SELECT *表示在DocumentDB中返回所有列。这意味着您希望返回结果中的每个文档与存储在数据库中的文档完全一样。

但是,当您选择特定的属性和表达式而不是简单地发出SELECT *时,您将为结果中的每个文档投影所需的新形状。

步骤3-单击“运行”以执行查询并打开结果刀片。

开放结果刀片

可以看出,检索了Wakefield家族,Smith家族和Anderson家族。

以下是由于SELECT * FROM c查询而检索到的三个文档。

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

但是,这些结果还包括系统生成的属性,所有属性均以下划线字符为前缀。