不重复计数不适用于cosmosdb

时间:2018-10-22 07:05:08

标签: azure azure-cosmosdb azure-cosmosdb-sqlapi

下面的不同查询现在可以使用:

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"}

1 个答案:

答案 0 :(得分:1)

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

希望它对您有帮助。