为什么Document DB过程在querydocument上只返回100个文档?

时间:2017-02-13 13:25:05

标签: stored-procedures azure-cosmosdb

我在Document DB中有以下程序。它从DocumentDb脚本资源管理器执行得很好,但它返回的结果是部分的。我有超过250个文件满足我在查询资源管理器中检查的给定where子句。但是当我从脚本资源管理器运行过程时,计数(在过程中定义)始终为100.

以下是我的程序 -

function getInvoice(pageNo, numberOfRecords, member, searchText, customerGroupId,ResellerId) {
var collectionReseller = getContext().getCollection();
var filterquery ;
var count=0, invoiceAmountTotal=0, referalCommissionTotal=0, developerCommissionTotal=0;
var customerIdString='';
var InvoiceList = [];
var whereClause;
if (customerGroupId != "") {
filterquery = 'SELECT c.id from c where c.Type="Customer" and c.CustomerGroupID="' + customerGroupId + '"';
var isAccepted = collectionReseller.queryDocuments(
    collectionReseller.getSelfLink(), filterquery,
    function (err, documents, responseOptions) {
        var docCount = documents.length;
        documents.forEach(function (doc) {
            docCount--;
            if (docCount > 0)
                customerIdString = customerIdString + '"' + doc.id + '", '
            else
                customerIdString = customerIdString + '"' + doc.id + '" '
        })
        whereClause = 'where  r.Type="Invoice" and r.CustomerID IN (' + customerIdString + ')'

        var filterquery1 = 'SELECT * FROM root r  ';
        if (member.length > 0) {

            member.forEach(function (val, i) {
                whereClause = whereClause + ' and contains(r.' + member[i] + ',"' + searchText[i] + '")';
            });
        }
        isAccepted = collectionReseller.queryDocuments(
         collectionReseller.getSelfLink(), filterquery1 + whereClause,
         function (err, invoiceDoc) {
             var qr = filterquery1 + whereClause;
        count = invoiceDoc.length;
             invoiceDoc.forEach(function (doc) {
                 invoiceAmountTotal = parseFloat(invoiceAmountTotal) + parseFloat(doc.InvoiceAmount);
                 referalCommissionTotal = parseFloat(referalCommissionTotal) + parseFloat(doc.ReferralCommission);
                 developerCommissionTotal= parseFloat(developerCommissionTotal) + parseFloat(doc.DeveloperCommission);
                 InvoiceList.push(doc);
             });


             InvoiceList.sort(SortByID);
             InvoiceList = InvoiceList.slice(pageNo * numberOfRecords, pageNo * numberOfRecords + numberOfRecords);
             // Check the feed and if empty, set the body to 'no docs found', 
             // else take 1st element from feed
             getContext().getResponse().setBody(JSON.stringify({ InvoiceList, count, invoiceAmountTotal, referalCommissionTotal, developerCommissionTotal }));

         });
    });
}
else
{   
    whereClause = ' where r.Type = "Invoice" and r.ResellerID = "'+ ResellerId + '"';

    filterquery = 'SELECT * FROM root r ';
    if(member.length > 0) {
    member.forEach(function (val, i) {
        whereClause = whereClause + ' and contains(r.' + member[i] + ',"' + searchText[i] + '")';
    });
}


 filterquery = filterquery + whereClause;

 var isAccepted = collectionReseller.queryDocuments(
    collectionReseller.getSelfLink(), filterquery,
    function (err, documents, responseOptions) {
        if (err) throw err;
        invoiceDoc = documents;
count =invoiceDoc.length;
            invoiceDoc.forEach(function (doc) {
                InvoiceList.push(doc);
                invoiceAmountTotal = parseFloat(invoiceAmountTotal) + parseFloat(doc.InvoiceAmount);
                 referalCommissionTotal = parseFloat(referalCommissionTotal) + parseFloat(doc.ReferralCommission);
                 developerCommissionTotal= parseFloat(developerCommissionTotal) + parseFloat(doc.DeveloperCommission);
            });
        InvoiceList.sort(SortByID);
        InvoiceList = InvoiceList.slice(pageNo * numberOfRecords, pageNo * numberOfRecords + numberOfRecords);
        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        getContext().getResponse().setBody(JSON.stringify({ InvoiceList, count, invoiceAmountTotal, referalCommissionTotal, developerCommissionTotal }));

    });


}
function SortByID(a, b) {
    var aName = a.UpdatedOn.toLowerCase();
    var bName = b.UpdatedOn.toLowerCase();
    return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

任何帮助都将受到高度赞赏..

1 个答案:

答案 0 :(得分:4)

如果您希望一次性取回所有250个,则需要使用queryDocuments()字段填充pageSize的options参数。它是该函数调用的可选第三个参数。没有它,这个服务器端API将默认为100。

您还可以将pageSize设置为-1以获取所有文档。但是,对于服务器端存储过程,我建议不要这样做。相反,您需要使用延续令牌处理分页。如果您希望它非常强大,您还需要处理存储过程的过早关闭。