我可以按任何属性对DocumentDB中的数据进行排序吗?

时间:2015-04-22 06:57:43

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

我想从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"}

从这个例外中,我理解该集合应该被某些属性编入索引,并且我的集合没有编入索引,

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

有人可以帮助摆脱这个问题吗?

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

下面的解决方案适用于通过属性获取排序列表,但是当我想通过子属性获取排序列表顺序时(例如:s.Name.FirstName其中Name是包含三个属性FirstName的属性, MiddleName和LastName)无效。

有人可以帮我这么做吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

是的,可以通过在整个集合中指定范围索引来实现,如下所示。有关详细信息,请参阅此处的文档: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);