CosmosDB存储过程未返回所有文档 [英] CosmosDB stored procedure not returning all documents

查看:44
本文介绍了CosmosDB存储过程未返回所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的存储过程,该过程向许多文档返回0.这是代码:

i have a pretty simple stored procedure that returns 0 to many documents. Here is the code:

function GetAllDocuments(numberOfDays){

  var context = getContext();
  var response = context.getResponse();
  var collection = context.getCollection();
  var collectionLink = collection.getSelfLink();

  var today  = new Date();  
  today.setDate(today.getDate() - numberOfDays);
  var inSeconds = today.getTime() / 1000; 
  var filterQuery = 'SELECT * FROM c WHERE c._ts >' + inSeconds;

  console.log(filterQuery);
  collection.queryDocuments(collectionLink, filterQuery, {pageSize:-1},
    function(err, documents) {
      response.setBody(response.getBody() + JSON.stringify(documents));
    }
  );
}

我面临的问题是,如果要返回许多文档,即20000,则不是所有文档都被返回.我认为我遇到了需要传递continuationToken的问题.我已经阅读了其他文章,指出我们需要返回至client(c#),然后再次调用传递令牌的sproc.我还没有找到代码示例.这也是确保全额回报的唯一方法吗?仅仅是存储过程问题吗?仅使用client.CreateDocumentQuery可以在一次调用中返回所有记录的情况下,我会更好吗?

The issue I am facing is, if there are many documents to be returned ie 20000 not all documents are returned. I think i am running into the issue where i need to pass a continuationToken. I have read other posts that states that we will need to return to client(c#) then make another call to the sproc passing in a token. I havent been able to find a code sample. Also is that the only way to ensure full return? Is it just a stored procedure issue? Will i be better off to just use client.CreateDocumentQuery that will simply return all records in one call?

感谢您的任何反馈!

这是使用杰伊的示例的新代码:

This is the new code using Jay's sample:

function GetAllDocumentsNew(numberOfDays) {

       var collection = getContext().getCollection();
       var collectionLink = collection.getSelfLink();
       var response = getContext().getResponse();
       var docCount = 0;
       var counter = 0;
       var returnArray = [];

       var today  = new Date();  
       today.setDate(today.getDate() - numberOfDays);
       var inSeconds = today.getTime() / 1000; 
       //var filterQuery = 'SELECT * FROM c WHERE c._ts >' + inSeconds;
       var filterQuery = "select * from c where c._ts > 1531763849.225 and c._ts <1532637743.261 and c.ProcessTypeID = 1";

       console.log(filterQuery);

       tryQuery();

       function tryQuery(continuation) {
            var query = {
                query: filterQuery
            };

            var requestOptions = {
                MaxItemCount: 10000,
                continuation: continuation
            };

            var isAccepted =
                collection
                .queryDocuments(collectionLink,
                                query,
                                requestOptions,
                                function queryCallback(err, documents,responseOptions) {
                                         if (err) throw err;
                                         if (documents.length > 0) {

                                            docCount = documents.length;
                                            console.log(docCount.toString());
                                            for (var i=0; i<docCount; i++){
                                                returnArray.push(documents[i]);
                                            }

                                           getContext().getResponse().setBody(returnArray);
                                          }
                                          else if (responseOptions.continuation) {
                                              // Else if the query came back empty, but with a continuation token; 
                                              // repeat the query with the token.
                                            tryQuery(responseOptions.continuation);
                                          } else {
                                                 throw new Error("Document not found.");
                                                 }
                                });

            if (!isAccepted) {
                throw new Error("The stored procedure timed out");
            }
        }
    }

推荐答案

如果要返回的文档很多,即不是所有文档都为20000 返回.我认为我遇到了我需要通过的问题 一个continuationToken.

if there are many documents to be returned ie 20000 not all documents are returned. I think i am running into the issue where i need to pass a continuationToken.

当然,当查询数据量很大时,您需要考虑在存储过程中使用延续令牌.您可以参考以下伪代码:

When the query data is quite huge,of course, you need to consider use continuation token in stored procedure. You could refer to the pseudo-code as below:

function GetAllDocuments(numberOfDays) {

       var collection = getContext().getCollection();
       var collectionLink = collection.getSelfLink();
       var response = getContext().getResponse();
       var docCount = 0;
       var counter = 0;
       var returnArray = [];

       var today  = new Date();  
       today.setDate(today.getDate() - numberOfDays);
       var inSeconds = today.getTime() / 1000; 
       var filterQuery = 'SELECT * FROM c WHERE c._ts >' + inSeconds;

       tryQuery();

       function tryQuery(continuation) {
            var query = {
                query: "select * from root r"
            };

            var requestOptions = {
                MaxItemCount: 1000
                continuation: continuation
            };

            var isAccepted =
                collection
                .queryDocuments(collectionLink,
                                query,
                                requestOptions,
                                function queryCallback(err, documents,responseOptions) {
                                         if (err) throw err;
                                         if (documents.length > 0) {

                                            docCount = documents.length;
                                            for (var i=0; i<docCount; i++){
                                                returnArray.push(documents[i]);
                                            }

                                           getContext().getResponse().setBody(returnArray);
                                          }
                                          else if (responseOptions.continuation) {
                                              // Else if the query came back empty, but with a continuation token; 
                                              // repeat the query with the token.
                                            tryQuery(responseOptions.continuation);
                                          } else {
                                                 throw new Error("Document not found.");
                                                 }
                                });

            if (!isAccepted) {
                throw new Error("The stored procedure timed out");
            }
        }
    }

我还阅读了其他文章,指出我们需要返回至 然后client(c#)再次调用传递令牌的sproc.一世 还没有找到代码示例.

I have read other posts that states that we will need to return to client(c#) then make another call to the sproc passing in a token. I havent been able to find a code sample.

据我所知,存储过程的运行限制为5秒(

As I know, stored procedure has 5 seconds running limitation(Is it possible to disable the 5 second time limit for Azure CosmosDB stored procedures, if your stored procedure time out,you need to call [ExecuteStoredProcedureAsync][1] on your client side and pass the continuation token as parameter.

我会更好地只使用client.CreateDocumentQuery 只需一次调用即可返回所有记录?

Will i be better off to just use client.CreateDocumentQuery that will simply return all records in one call?

据我所知,存储过程是在服务器端运行的JS代码脚本.我认为性能比使用客户端调用SDK更有效.但是,它受JS语法和运行时间的限制.如果您不能忍受这些功能,建议您通过客户端SDK完成要求.

Per my knowledge,stored procedures are JS code scripts which running on the server side.I presume that performance is more efficient than using client invoke SDK. However, it is limited to the JS syntax and running time.If you can not tolerate these features,i suggest you could complete your requirements via client SDK.

希望它对您有所帮助.有任何问题,请告诉我.

Hope it helps you.Any concern ,please let me know.

这篇关于CosmosDB存储过程未返回所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆