4
votes

I've been reading the limited documentation and from what I've seen, this query should work.

// SAMPLE STORED PROCEDURE
function sample() {
var prefix = "";
var collection = getContext().getCollection();
var data = collection.readDocuments(collection.getSelfLink());
console.log(data);
console.log(JSON.stringify(data));
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
    collection.getSelfLink(),
    'SELECT * FROM root r',
function (err, feed, options) {
    if (err) throw err;

    // Check the feed and if empty, set the body to 'no docs found', 
    // else take 1st element from feed
    if (!feed || !feed.length) {
        var response = getContext().getResponse();
        response.setBody('no docs found');
    }
    else {
        var response = getContext().getResponse();
        var body = { prefix: prefix, feed: feed[0] };
        response.setBody(JSON.stringify(body));
    }
});

if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

After all, this is just the sample query that the portal generates. The only issue is that it does not return any results.

  • The collection has 7000 documents
  • It is partitioned by a prop type /EmailTypeId
  • When I execute the query I am submitting partition value of 5 (which is the partition value for all current records)
  • I'm console.logging a call to collection.readDocuments which should return all docs but it just returns the value true

I'm trying to return all records so I can aggregate by id and count. How can I get this query to actually return data?

Here is a sample screen shot of one of the doc schemas in the collection
enter image description here Here is the input form for supplying the partition value enter image description here

Update

I created a new collection as a control. This collection has no partition key and it seems the same query returns results in that collection. Therefor the issue has to be in the 2nd screen shot I provided. Perhaps I am providing the partition key incorrectly.

1
I feel your pain... shouldn't be this difficult. - willem

1 Answers

0
votes

I believe you need to limit your response size since there is a limit coming from cosmo. I added something like this in my sproc to alleviate this:

if (responseSize + queryPageSize < 1024 * 1024) {
      // Append query results to nodesBatch.
      nodesBatch = nodesBatch.concat(documentsRead);

      // Keep track of the response size.
      responseSize += queryPageSize;

      if (responseOptions.continuation) {
        // If there is a continuation token... Run the query again to get the next page of results
        lastContinuationToken = responseOptions.continuation;
        getNodes(responseOptions.continuation);
      } else {
        // If there is no continutation token, we are done. Return the response.
        response.setBody({
          "message": "Query completed succesfully.",
          "queryResponse": nodesBatch
        });
      }
    } else {
      // If the response size limit reached; run the script again with the lastContinuationToken as a script parameter.
      response.setBody({
        "message": "Response size limit reached.",
        "lastContinuationToken": lastContinuationToken,
        "queryResponse": nodesBatch
      });
    }
  });

Let me know how this works for you.

Also, check your collection name and use that instead of root