当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Can I sort the data in DocumentDB by any attribute?

问题描述:

I want to get the ordered data from documentDB query, ofcourse, I know that DocumentDB is not supporting the orderBY or sorting concept, However one can write stored procedure to get sorted list,

my query is something like

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

where I want to get sorted list order by age.

when I am trying to run stored procedure, it is raising an aggregate exception something like

{"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"}

from this exception, I understood that the collection should be indexed by some property, and my collection is not indexed,

for this I can apply Indexing for my collection, but It might not work if I want to get the sorted list ordered by some other property of name,

Can somebody help to get rid of this problem?

and my straight question is "is it possible to get sorted list(ascending/descending) from documentdb based on any property using stored procedures?"

Below solution is working fine for getting the sorted list by the property, but when I want to get the sorted list order by the sub property (ex : s.Name.FirstName where Name is a property which contains three properties FirstName, MiddleName and LastName) not working.

can someone help me to do this?

thanks in advance

网友答案:

Yes, this is possible by specifying a range index over the entire collection as shown below. For more details, refer to the docs here: 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);   
分享给朋友:
您可能感兴趣的文章:
随机阅读: