【问题标题】:Can I sort the data in DocumentDB by any attribute?我可以按任何属性对 DocumentDB 中的数据进行排序吗?
【发布时间】:2015-04-22 06:57:42
【问题描述】:

我想从documentDB查询中获取有序数据,当然我知道DocumentDB不支持orderBY或排序概念,但是可以编写存储过程来获取排序列表,

我的查询类似于

SELECT root.id FROM Root root WHERE (root.age< 30)

我想在哪里按年龄排序。

当我尝试运行存储过程时,它会引发类似的聚合异常

{"Exception: Microsoft.Azure.Documents.BadRequestException, message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: {\\\"Errors\\\":[\\\"An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request.\\\"]}\\r\\nStack trace: Error: {\\\"Errors\\\":[\\\"An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request.\\\"]}\\n   at callback (OrderBy.js:54:13)\\n   at Anonymous function (OrderBy.js:521:29)\"]}, request URI: rntbd://10.98.107.60:14900/apps/4c8d65d7-216b-46b4-abb7-52c1a0c7123f/services/appcrest-ServerService-1/partitions/cf963206-7d13-4b94-9f03-06954e03f667/replicas/130737244172846540p\r\nActivityId: 7aeab81e-db33-4a7d-9bb6-172966d9cc60"}

从这个异常中,我了解到集合应该被某个属性索引,而我的集合没有被索引,

为此,我可以为我的集合应用索引,但如果我想获取按名称的其他属性排序的排序列表,它可能不起作用,

有人可以帮忙解决这个问题吗?

我的直接问题是“是否可以使用存储过程根据任何属性从 documentdb 获取排序列表(升序/降序)?”

下面的解决方案可以很好地按属性获取排序列表,但是当我想通过子属性获取排序列表顺序时(例如:s.Name.FirstName,其中 Name 是一个包含三个属性 FirstName 的属性, MiddleName 和 LastName) 不起作用。

有人可以帮我做这件事吗?

提前致谢

【问题讨论】:

    标签: c# sorting stored-procedures azure-cosmosdb


    【解决方案1】:

    是的,这可以通过在整个集合上指定一个范围索引来实现,如下所示。有关详细信息,请参阅此处的文档:http://azure.microsoft.com/documentation/articles/documentdb-indexing-policies/

    var rangeDefault = new DocumentCollection { Id = "rangeCollection" };                                                              
    rangeDefault.IndexingPolicy.IncludedPaths.Add(
    new IndexingPath {
        IndexType = IndexType.Range, 
            Path = "/",
            NumericPrecision = 7 });
    
    rangeDefault = await client.CreateDocumentCollectionAsync(
     database.SelfLink, 
    rangeDefault);   
    

    【讨论】:

    • 我的集合有大约 3-40 万个文档,我使用了所有三个集合,而不会丢失数据,我该如何实现上述内容?它可以在任何领域订购吗? (doc.sub-doc.property) ? @aravind
    • 您的解决方案适用于 orderby 属性,但不适用于子属性 :(
    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多