我使用Azure DocumentDB存储有关用户的信息。这些文档包含一个名为 date_created 的属性。我想向documentDB发送一个查询,以检索存储在此集合中的所有用户。我也想要从最新到最旧订购结果。
我目前在DocumentDB中实现了一个存储过程,该过程遍历集合并根据date属性对结果进行排序。但是,我有两个问题:
据我所知,DocumentDB尚不支持排序控制,但如果我使用存储过程来排序结果,我是执行完整的集合扫描,还是数据库至少利用属性的索引我正在排序?有关信息,我使用的商店程序就是这个(从产品团队提供的Azure示例中稍作修改):
function orderBy(filterQuery, orderByFieldName, continuationToken) {
// HTTP error codes sent to our callback funciton by DocDB server.
var ErrorCode = {
REQUEST_ENTITY_TOO_LARGE: 413,
}
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var result = new Array();
tryQuery({});
function tryQuery(options) {
var isAccepted = (filterQuery && filterQuery.length) ?
collection.queryDocuments(collectionLink, filterQuery, options, callback) :
collection.readDocuments(collectionLink, options, callback)
if (!isAccepted) throw new Error("Source dataset is too large to complete the operation.");
}
/**
* queryDocuments callback.
* @param {Error} err - Error object in case of error/exception.
* @param {Array} queryFeed - array containing results of the query.
* @param {ResponseOptions} responseOptions.
*/
function callback(err, queryFeed, responseOptions) {
if (err) {
throw err;
}
// Iterate over document feed and store documents into the result array.
queryFeed.forEach(function (element, index, array) {
result[result.length] = element;
});
if (responseOptions.continuation) {
// If there is continuation, call query again providing continuation token.
tryQuery({ continuation: responseOptions.continuation });
} else {
// We are done with querying/got all results. Sort the results and return from the script.
result.sort(compare);
fillResponse();
}
}
// Compare two objects(documents) using field specified by the orderByFieldName parameter.
// Return 0 if equal, -1 if less, 1 if greater.
function compare(x, y) {
if (x[orderByFieldName] == y[orderByFieldName]) return 0;
else if (x[orderByFieldName] < y[orderByFieldName]) return 1;
return -1;
}
// This is called in the very end on an already sorted array.
// Sort the results and set the response body.
function fillResponse() {
var page_size = 20;
// Main script is called with continuationToken which is the index of 1st item to start result batch from.
// Slice the result array and discard the beginning. From now on use the 'continuationResult' var.
var continuationResult = result;
if (continuationToken) continuationResult = result.slice(continuationToken);
else continuationToken = 0;
if (page_size > continuationResult.length ) {
page_size = continuationResult.length;
}
// Get/initialize the response.
var response = getContext().getResponse();
response.setBody(null);
// Take care of response body getting too large:
// Set Response iterating by one element. When we fail due to MAX response size, return to the client requesting continuation.
var i = 0;
var final_result = [];
for (; i < page_size; ++i) {
try {
// Note: setBody is very expensive vs appendBody, use appendBody with simple approximation JSON.stringify(element).
final_result.push(continuationResult[i]);
} catch (ex) {
if (!ex.number == ErrorCode.REQUEST_ENTITY_TOO_LARGE) throw ex;
break;
}
}
/* Now next batch to return to client has i elements.
// Slice the continuationResult if needed and discard the end. */
var partialResult = continuationResult;
var newContinuation = null;
if (i < continuationResult.length) {
partialResult = continuationResult.slice(0, i);
}
// Finally, set response body.
response.setBody({ result: final_result, continuation: i });
}
}
答案 0 :(得分:3)
更新:截至2015年7月 - DocumentDB支持查询中的ORDER BY
目前,您需要在客户端或存储过程中对集合进行排序(尽管我建议在客户端上执行此操作,因为存储过程已执行有限的操作)。< / p>
回答你的问题:
1)我不相信有保证的默认排序
2)存储过程将导致集合扫描,然后对其进行排序。
你可以帮助推动&#34; Order By&#34;通过在Azure反馈论坛上发表您的意见: http://feedback.azure.com/forums/263030-documentdb/suggestions/6334829--order-by-in-queries