在本章中,我们将介绍FROM子句,它与常规SQL中的标准FROM子句不同.
查询始终在特定集合的上下文中运行,无法加入跨集合中的文档,这让我们想知道为什么我们需要一个FROM子句.事实上,我们没有,但如果我们不包含它,那么我们就不会在集合中查询文档.
此子句的目的是指定数据查询必须运行的源.通常整个集合是源,但可以指定集合的子集. FROM< from_specification>除非在查询中稍后过滤或投影源,否则子句是可选的.
让我们再看一下相同的例子.以下是 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 "表示整个家庭col lection是枚举的源.
源也可以缩减为更小的子集.当我们只想在每个文档中检索一个子树时,子根可以成为源,如下例所示.
当我们运行以下查询 :
SELECT * FROM Families.parents
将检索以下子文档.
[ [ { "familyName": "Wakefield", "givenName": "Robin" }, { "familyName": "Miller", "givenName": "Ben" } ], [ { "familyName": "Smith", "givenName": "James" }, { "familyName": "Curtis", "givenName": "Helen" } ], [ { "firstName": "Thomas", "relationship": "father" }, { "firstName": "Mary Kay", "relationship": "mother" } ] ]
由于此查询,我们可以看到只检索父母子文档.