【问题标题】:how to select from collection with guid id?如何从带有 guid id 的集合中选择?
【发布时间】:2017-01-10 12:12:40
【问题描述】:

我有这个带有 guid id 的集合。当我使用 Azure Query Explorer 并在上面的组合框中选择了集合时,没关系:

SELECT * FROM c

但是当我尝试这样选择它时(来自查询 exlorer 和 c# 代码):

SELECT * FROM 357fa002-dc7d-4ede-935a-6a0c80cf9239 c

我明白了:

语法错误,无效的数值标记“357fa002”。

我尝试将它放在 guid 周围的引号(单引号和双引号)中,但没有成功..

Microsoft 文档声明此集合 ID 不违反任何规则: https://docs.microsoft.com/pl-pl/azure/documentdb/documentdb-create-collection

集合名称必须介于 1 到 255 个字符之间,并且不能包含 / \ # ?或尾随空格

如何使用它的 id 查询这个集合?

【问题讨论】:

    标签: c# azure azure-cosmosdb


    【解决方案1】:

    根据你的描述,我查了DocumentDB SQL Syntax的FROM clause的官方文档。以下是上述文档的语法:

    FROM <from_specification>  
    
    <from_specification> ::=   
            <from_source> {[ JOIN <from_source>][,...n]}  
    
    <from_source> ::=   
              <collection_expression> [[AS] input_alias]  
            | input_alias IN <collection_expression>  
    
    <collection_expression> ::=   
            ROOT   
         | collection_name  
         | input_alias  
         | <collection_expression> '.' property_name  
         | <collection_expression> '[' "property_name" | array_index ']' 
    

    对于&lt;collection_expression&gt;,我们可以指定当前连接的集合的名称。

    根据您的情况,我在我这边进行了尝试并重现了此问题。另外,我测试了这个问题,发现input_aliascollection_name只能由数字和字母组成,并且第一个必须是字母。在我的测试中,我假设你到现在都无法达到这个目的。我会报告这个问题,您可以添加您的反馈here

    更新:

    对于 C# 的客户端库 Microsoft Azure DocumentDB,我们可以在调用 DocumentClient.CreateDocumentQuery 时利用 Fiddler 捕获请求,而无需指定 sqlExpression 参数,如下所示:

    var items =
        client.CreateDocumentQuery<GroupedSales>(UriFactory.CreateDocumentCollectionUri(DatabaseId,
            DocumentCollectionId)).Where(
                x =>
                    x.Date >= filter.From.Date
                    && x.Date <= filter.To.Date
                    ).ToList();
    

    注意:如果没有Where 子句,请求正文将为空。此时查询等于“SELECT * FROM root”。

    【讨论】:

    • 还有更多,显然我可以像这样查询这个集合:var elements query = client.CreateDocumentQuery&lt;T&gt;(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)).ToList(); 所以我想这毕竟是可能的。但是当我在失败时明确提供 sql:IDocumentQuery&lt;T&gt; query = client.CreateDocumentQuery&lt;T&gt;(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), string.Format("SELECT * FROM {0}", CollectionId)).ToList();
    • 很高兴听到这个消息。我已经报告了这个问题,您也可以添加您的feedback
    【解决方案2】:

    在 DocumentDB 中,查询的范围是单个集合(而不是关系数据库中的数据库)。在 REST API 中,查询是针对集合 URI /dbs/&lt;my-db&gt;/colls/&lt;my0coll&gt;POST 请求。

    在 Azure 门户的查询资源管理器中,您必须在下拉列表中选择数据库/集合,然后对其进行查询。

    【讨论】:

      【解决方案3】:

      Bruce MSFT 解决了这个难题。 SELECT * FROM 357fa002-dc7d-4ede-935a-6a0c80cf9239 c 可以这样执行:

      var collectionId = "357fa002-dc7d-4ede-935a-6a0c80cf9239"
      var uri = UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId);
      var sql = string.Format("SELECT * FROM root c", attributeName);
      var elements = client.CreateDocumentQuery(uri, sql).ToList();
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        • 2014-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多