不同的计数不适用于 cosmos db

Count of distinct doesnot work with cosmosdb

下面的不同查询现在有效:

SELECT DISTINCT c.name  FROM c

但是下面的查询不起作用,这里有什么地方做错了吗?

SELECT COUNT(DISTINCT c.name) FROM c

Failed to query documents for collection products: {"code":400,"body":"{\"code\":\"BadRequest\",\"message\":\"Message: {\\"errors\\":[{\\"severity\\":\\"Error\\",\\"location\\":{\\"start\\":13,\\"end\\":21},\\"code\\":\\"SC1001\\",\\"message\\":\\"Syntax error, incorrect syntax near 'DISTINCT'.\\"}]}\r\nActivityId: 05bf1757-ce1b-4ee4-9ae5-f2b778c55ded, Microsoft.Azure.Documents.Common/2.0.0.0\"}","activityId":"05bf1757-ce1b-4ee4-9ae5-f2b778c55ded"}

SELECT COUNT(DISTINCT c.name) FROM c 是 Cosmos DB 不支持的语法错误。

您可以使用 Stored Procedure 为查询结果执行计数任务:

function sample() {
    var collection = getContext().getCollection();
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT distinct r.name FROM root r',
    function (err, feed, options) {
        if (err) throw err;

        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
            //or return 0 
        }
        else {
            var response = getContext().getResponse();
            response.setBody(feed.length);
        }
    });

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