Azure Cosmos DB 中的 FROM 子句FROM clause in Azure Cosmos DB

FROM (FROM <from_specification>) 子句是可选的,除非稍后在查询中对源进行筛选或投影。The FROM (FROM <from_specification>) clause is optional, unless the source is filtered or projected later in the query. SELECT * FROM Families 之类的查询枚举整个 Families 容器。A query like SELECT * FROM Families enumerates over the entire Families container. 还可以对容器使用特殊标识符 ROOT,而无需使用容器名称。You can also use the special identifier ROOT for the container instead of using the container name.

FROM 子句对每个查询强制实施以下规则:The FROM clause enforces the following rules per query:

  • 容器可以使用别名,如 SELECT f.id FROM Families AS f 或只需为 SELECT f.id FROM Families fThe container can be aliased, such as SELECT f.id FROM Families AS f or simply SELECT f.id FROM Families f. 此处的 fFamilies 的别名。Here f is the alias for Families. AS 是可选的关键字,用于指定标识符的别名AS is an optional keyword to alias the identifier.

  • 指定别名后,无法绑定原始的源名称。Once aliased, the original source name cannot be bound. 例如,SELECT Families.id FROM Families f 在语法上是无效的,原因是标识符 Families 已指定别名,因此不再可以解析。For example, SELECT Families.id FROM Families f is syntactically invalid because the identifier Families has been aliased and can't be resolved anymore.

  • 所有被引用的属性必须完全限定,以避免在不严格遵守架构时出现任何有歧义的绑定。All referenced properties must be fully qualified, to avoid any ambiguous bindings in the absence of strict schema adherence. 例如,SELECT id FROM Families f 在语法上是无效的,因为未绑定属性 idFor example, SELECT id FROM Families f is syntactically invalid because the property id isn't bound.

语法Syntax

FROM <from_specification>  

<from_specification> ::=
        <from_source> {[ JOIN <from_source>][,...n]}  

<from_source> ::=
          <container_expression> [[AS] input_alias]  
        | input_alias IN <container_expression>  

<container_expression> ::=
        ROOT
     | container_name  
     | input_alias  
     | <container_expression> '.' property_name  
     | <container_expression> '[' "property_name" | array_index ']'  

参数Arguments

  • <from_source>

    指定数据源,可以带别名,也可以不带别名。Specifies a data source, with or without an alias. 如果未指定别名,则会使用以下规则从 <container_expression> 推断别名:If alias is not specified, it will be inferred from the <container_expression> using following rules:

    • 如果表达式为 container_name,那么 container_name 将用作别名。If the expression is a container_name, then container_name will be used as an alias.

    • 如果表达式是 <container_expression>,则会将 property_name 用作别名。If the expression is <container_expression>, then property_name, then property_name will be used as an alias. 如果表达式为 container_name,那么 container_name 将用作别名。If the expression is a container_name, then container_name will be used as an alias.

  • AS input_aliasAS input_alias

    指定 input_alias 为基础容器表达式返回的一组值。Specifies that the input_alias is a set of values returned by the underlying container expression.

  • input_alias INinput_alias IN

    指定 input_alias 应表示通过遍历由基础容器表达式返回的每个数组的所有数组元素获得的一组值。Specifies that the input_alias should represent the set of values obtained by iterating over all array elements of each array returned by the underlying container expression. 基础容器表达式返回的非数组的任何值都将被忽略。Any value returned by underlying container expression that is not an array is ignored.

  • <container_expression>

    指定要用于检索文档的容器表达式。Specifies the container expression to be used to retrieve the documents.

  • ROOT

    指定应从当前连接的默认容器中检索文档。Specifies that document should be retrieved from the default, currently connected container.

  • container_name

    指定应从当前提供的容器中检索文档。Specifies that document should be retrieved from the provided container. 容器的名称必须匹配当前连接到的容器名称。The name of the container must match the name of the container currently connected to.

  • input_alias

    指定应当从由提供的别名定义的其他源检索文档。Specifies that document should be retrieved from the other source defined by the provided alias.

  • <container_expression> '.' property_name

    指定应通过访问 property_name 属性来检索文档。Specifies that document should be retrieved by accessing the property_name property.

  • <container_expression> '[' "property_name" | array_index ']'

    指定应通过访问由指定容器表达式检索的所有文档的 property_name 属性或 array_index 数组元素来检索文档。Specifies that document should be retrieved by accessing the property_name property or array_index array element for all documents retrieved by specified container expression.

备注Remarks

<from_source>( 中提供或推断出的所有别名都必须是唯一的。All aliases provided or inferred in the <from_source>(s) must be unique. 语法 <container_expression>.property_name 与 <container_expression>' ['"property_name"']' 相同。The Syntax <container_expression>.property_name is the same as <container_expression>' ['"property_name"']'. 但是,如果属性名称包含非标识符字符,则可以使用后一种语法。However, the latter syntax can be used if a property name contains a non-identifier character.

处理未命中属性、未命中数组元素和未定义值Handling missing properties, missing array elements, and undefined values

如果某个容器表达式访问属性或数组元素,但该值不存在,将忽略该值,且不会做进一步处理。If a container expression accesses properties or array elements and that value does not exist, that value will be ignored and not processed further.

容器表达式上下文范围Container expression context scoping

容器表达式的范围可以是集合或文档:A container expression may be container-scoped or document-scoped:

  • 如果容器表达式的基础源是 ROOT 或 container_name,则表达式的范围是容器。An expression is container-scoped, if the underlying source of the container expression is either ROOT or container_name. 此类表达式表示从容器中直接检索的一组文档,且不依赖于其他容器表达式的处理。Such an expression represents a set of documents retrieved from the container directly, and is not dependent on the processing of other container expressions.

  • 如果容器表达式的基础源是之前在查询中引入的 input_alias,则表达式的范围是文档。An expression is document-scoped, if the underlying source of the container expression is input_alias introduced earlier in the query. 此类表达式表示通过计算每个文档范围内的容器表达式获得的一组文档,该文档属于与别名容器相关联的集。Such an expression represents a set of documents obtained by evaluating the container expression in the scope of each document belonging to the set associated with the aliased container. 结果集为多个集的合并,通过计算基础集中每个文档的容器表达式获得。The resulting set will be a union of sets obtained by evaluating the container expression for each of the documents in the underlying set.

示例Examples

使用 FROM 子句获取子项Get subitems by using the FROM clause

FROM 子句可将源化简为更小的子集。The FROM clause can reduce the source to a smaller subset. 要在每个项中仅枚举子树,子根可能会变成源,如以下示例所示:To enumerate only a subtree in each item, the subroot can become the source, as shown in the following example:

    SELECT *
    FROM Families.children

结果有:The results are:

    [
      [
        {
            "firstName": "Henriette Thaulow",
            "gender": "female",
            "grade": 5,
            "pets": [
              {
                  "givenName": "Fluffy"
              }
            ]
        }
      ],
      [
       {
            "familyName": "Merriam",
            "givenName": "Jesse",
            "gender": "female",
            "grade": 1
        },
        {
            "familyName": "Miller",
            "givenName": "Lisa",
            "gender": "female",
            "grade": 8
        }
      ]
    ]

上述查询使用数组作为源,但你也可以使用对象作为源。The preceding query used an array as the source, but you can also use an object as the source. 该查询考虑将源中任何有效的已定义 JSON 值包含在结果中。The query considers any valid, defined JSON value in the source for inclusion in the result. 以下示例将排除不带 address.state 值的 FamiliesThe following example would exclude Families that don't have an address.state value.

    SELECT *
    FROM Families.address.state

结果有:The results are:

    [
      "WA",
      "NY"
    ]

后续步骤Next steps