可以在服务器端同时进行排序和分页的 DocumentDb 查询吗?

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

我正在编写 API 以允许客户端使用按时间排序的数据。它有很多(每个客户端 10k+ 记录)所以我不想将所有这些转储回客户端并在那里订购,因此我需要订购它并在服务器端对其进行分页。我有分页功能,但看不到如何添加排序。

我看到了在客户端上排序的建议,但考虑到在这种情况下可能无法处理的数据量。有解决方法吗?

这是我目前的情况:

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

它不受开箱即用的支持,但您可以实现执行此操作的存储过程。

msft 产品组在此处提供了一些代码示例:https://code.msdn.microsoft.com/windowsazure/Azure-DocumentDB-NET-Code-6b3da8af/sourcecode?fileId=132409&pathId=34503205

查看服务器端脚本下的 JS 文件夹,您会看到执行此操作的 "orderby" 脚本。根据您的需要进行调整并尝试一下。

DocumentDB 现在正式支持 ORDER BY

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