如何从具有 guid id 的集合中 select?

how to select from collection with guid id?

我得到了带有 guid id 的这个集合。当我将 Azure 查询资源管理器与上面的组合框中的集合 selected 一起使用时,没问题:

SELECT * FROM c

但是当我尝试 select 时(来自查询 exlorer 和 c# 代码):

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

我得到:

Syntax error, invalid numeric value token '357fa002'.

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

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

Collection names must be between 1 and 255 characters, and cannot contain / \ # ? or a trailing space

如何使用它的 ID 查询此集合?

在 DocumentDB 中,查询的范围限定为单个集合(而不是像关系数据库中的数据库)。在 REST API 中,查询是针对集合 URI /dbs/<my-db>/colls/<my0coll>POST 请求。

在 Azure 门户的查询资源管理器中,您必须 select 下拉列表中的 database/collection,然后进行查询。

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

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 ']' 

对于<collection_expression>,我们可以指定当前连接的集合的名称。

根据你的情况,我自己试了一下,重现了这个问题。另外,我测试了这个问题,发现 input_aliascollection_name 只能由数字和字母组成,并且第一个必须是字母。经过我的测试,我假设你到现在还不能达到这个目的。我会报告这个问题,你可以添加你的反馈 here

更新:

对于客户端库 Microsoft Azure DocumentDB for C#, we could leverage Fiddler 在调用 DocumentClient.CreateDocumentQuery 时捕获请求而不指定 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".

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();

谢谢!