Cosmos DB存储过程返回无结果

时间:2018-05-27 23:10:27

标签: azure-cosmosdb

我一直在阅读有限的文档,而且从我所看到的情况来看,此查询应该有效。

// 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.');
}

毕竟,这只是门户生成的示例查询。唯一的问题是它不会返回任何结果。

  • 该集合有7000个文档
  • 它由道具类型/EmailTypeId
  • 划分
  • 当我执行查询时,我提交的分区值为5(这是所有当前记录的分区值)
  • 我在控制台。记录对collection.readDocuments的调用,该调用应返回所有文档,但只返回值true

我尝试返回所有记录,以便按ID和计数进行汇总。如何让此查询实际返回数据?

以下是集合中其中一个doc模式的示例屏幕截图
enter image description here 以下是提供分区值的输入表单 enter image description here

更新

我创建了一个新的集合作为控件。此集合没有分区键,似乎同一查询返回该集合中的结果。因此问题必须出在我提供的第二个屏幕截图中。也许我错误地提供了分区密钥。

1 个答案:

答案 0 :(得分:0)

我认为您需要限制您的响应大小,因为cosmo存在限制。我在我的sproc中添加了类似的内容来缓解这个问题:

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
      });
    }
  });

让我知道这对你有用。

另外,请检查您的收藏名称并使用该名称而不是root