无法从 Cosmos 存储过程中检索计数

Unable to retrieve count from Cosmos Stored Procedure

我无法根据提供给过程的过滤器查询检索文档计数。这是详细信息 -

SP 脚本

function count(filterQuery, continuationToken) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var maxResult = 25;
// The number of documents counted.
var result = 0;

tryQuery(continuationToken);

// Helper method to check for max result and call query.
function tryQuery(nextContinuationToken) {
    var responseOptions = { continuation: nextContinuationToken, pageSize: maxResult };
    if (result >= maxResult || !query(responseOptions)) {
        setBody(nextContinuationToken);
    }
}

function query(responseOptions) {
    var query = "SELECT VALUE COUNT(1) FROM c WHERE c.ActivityId='1816820'";
    return (query && query.length) ?
        collection.queryDocuments(collectionLink, query, responseOptions, onReadDocuments) :
        collection.readDocuments(collectionLink, responseOptions, onReadDocuments);
}

function onReadDocuments(err, queryFeed, responseOptions) {
    if (err) {
        console.length(err);
        throw 'Error while reading document: ' + err;
    }

    console.log(queryFeed);     //prints 0
    result += queryFeed[0];

    if (responseOptions.continuation) {
        tryQuery(responseOptions.continuation);
    } else {
        setBody(null);
    }
}

function setBody(continuationToken) {
    var body = { count: result, continuationToken: continuationToken };
    getContext().getResponse().setBody(body);
}

}

这是截图。可以肯定的是,我暂时在这里编写了硬编码的查询。否则我正在传递参数。

这里是直接查询结果-

请帮忙。

For partitioned containers, when executing a stored procedure, a partition key value must be provided in the request options. Stored procedures are always scoped to a partition key. Items that have a different partition key value will not be visible to the stored procedure.

正如MSDN所说,当你执行一个存储过程时,你需要提供分区键值。但是您提供的是分区键路径(“/ActivityId”),而不是分区键值。这就是为什么你得到 0 计数。所以你需要传递 "1816820" 作为分区键值来执行你的存储过程。像这样: