Rory McCrossan
Rory McCrossan

Reputation: 337580

DocumentDb query with both ordering and paging on the server side, possible?

I am writing an API to allow a client to consume time-ordered data. There is a lot of it (10k+ records per client) so I don't want to dump all of it this back to the client and order there, hence my need to both order it and page it server-side. I have the paging working, but I cannot see how to add ordering.

I've seen recommendations to sort on the client, but given the potential amount of data that is not going to work in this case. Is there a workaround?

Here is what I have so far:

var options = new FeedOptions {
    MaxItemCount = 25,
    RequestContinuation = continuationToken
}
var query = String.Format("SELECT * FROM TimelineEvent t WHERE t.acc_id = '{0}' AND t.removed != true", accountId); 
// ORDER BY in the query text doesn't appear to work
var events = client.CreateDocumentQuery<TimelineEvent>(colSelfLink, query, options).AsDocumentQuery();
var response = await query.ExecuteNextAsync<TimelineEvent>();

Upvotes: 1

Views: 2140

Answers (2)

Avinash Gadiraju
Avinash Gadiraju

Reputation: 723

ORDER BY is now officially supported by DocumentDB

https://azure.microsoft.com/en-us/documentation/articles/documentdb-orderby/

Upvotes: 1

Luis Delgado
Luis Delgado

Reputation: 3734

It's not supported out of the box, but you can implement a stored procedure that does this.

The msft product group has supplied some code samples here: https://code.msdn.microsoft.com/windowsazure/Azure-DocumentDB-NET-Code-6b3da8af/sourcecode?fileId=132409&pathId=34503205

Look under server side script, JS folder, you'll see an "orderby" script that does this. Adjust to your needs and try it.

Upvotes: 2

Related Questions