MaxItemCount feed options property in Cosmos DB doesnt work

I am trying to write a simple stored procedure which runs a SQL query and I want to restrict the results by using MaxItemCount property. Query produces 3 documents but I want to return only 1 document. I am forcing this restriction using MaxItemCount property but that doesn't seem to be working.

function uspGetDetailsByEmailId(EmailId) {
  var collection = getContext().getCollection();
  var collectionLink = collection.getSelfLink();
  var response = getContext().getResponse();
  var recordcount = 0;
  if (!EmailId) throw new Error("Please provide the EmailId for which     Details need to be retrieved");     

  retrieveDetails();     

  function retrieveDetails() {
    var requestOptions = {MaxItemCount: "1"};
    var dbquery = {query: "SELECT * FROM c"};
    var isAccepted = collection.queryDocuments(collectionLink, dbquery, requestOptions,function (err, results) {
      console.log(results.length)  
      response.setBody(results)
      if (err) throw err;                   
    });

    if (!isAccepted) {
      response.setBody(null);
    }
  }
}

1 answer

  • answered 2019-01-11 16:31 Nick Chapsas

    MaxItemCount is not a restriction on the amount of documents that the whole query will return.

    Cosmos DB always returns paged data. This means that if you get back 100 documents then there is a good change that these documents came back from many requests that had the same or almost the same amount of documents each. Then they are added together and returned.

    MaxItemCount is the maximum account of documents, a single trip to Cosmos DB will contain, but if you query data using the queryDocuments method then you will simply get the same amount of data but you will limit each trip to Cosmos DB to carrying a maximum of 1 document.

    What you would do in this case is use a query iterator and escape the iterator's loop once you have the first document.

    Looking more into this (mainly using the only documentation I can find https://azure.github.io/azure-cosmosdb-js-server) it looks like the js-server that the stored procedures are using is a different SDK all together.

    After digging into this even more, I found out that there is not MaxItemCount property in the RequestOptions object (and even if there was it would be a integer). However there is a property called pageSize as you can see in line 554 of https://azure.github.io/azure-cosmosdb-js-server/DocDbWrapperScript.js.html.

    After testing it, it looks like setting the pageSize to 1 will successfully limit your results to the desired amount.

    Here is the updated code:

    function uspGetDetailsByEmailId(EmailId) {
      var collection = getContext().getCollection();
      var collectionLink = collection.getSelfLink();
      var response = getContext().getResponse();
      var recordcount = 0;
      if (!EmailId) throw new Error("Please provide the EmailId for which     Details need to be retrieved");     
    
      retrieveDetails();     
    
      function retrieveDetails() {
        var requestOptions = { pageSize: 1};
        var dbquery = {query: "SELECT * FROM c"};
        var isAccepted = collection.queryDocuments(collectionLink, dbquery, requestOptions,function (err, results) {
          console.log(results.length)  
          response.setBody(results)
          if (err) throw err;                   
        });
    
        if (!isAccepted) {
          response.setBody(null);
        }
      }
    }