COUNT unique values with DISTINCT Subquery Expected Result in Data Explorer but not when using Rest API

muk 20 Reputation points
2023-06-23T23:27:15.0233333+00:00

Hello,

I have a question regarding varying results from running a query via REST API vs Data Explorer on the container directly.

Lets say I have a Cosmos DB container with the following items.

[
  {
    id: '1',
    position: 'Table flipper'
    city: 'Vancouver'
    type: 'Job'
  },
  {
    id: '2',
    position: 'Table painter'
    city: 'Seattle'
    type: 'Job'
  },  
  {
    id: '3',
    position: 'Table flipper'
    city: 'Calgary'
    type: 'NotJob'
  },
  {
    id: '4',
    position: 'Table maker'
    city: 'Toronto'
    type: 'Job'
  },
  {
    id: '5',
    position: 'Table maker'
    city: 'Vancouver'
    type: 'Job'
  },
  {
    id: '6',
    position: 'Table flipper'
    city: 'Vancouver'
    type: 'Job'
  },
]

What I want is the a count of the unique cities given a type.

The query I am running and tried is the following

SELECT VALUE COUNT(subCollection.city) FROM (
  SELECT DISTINCT (c.city) FROM c
  WHERE c.type = 'Job'
) as subCollection

I expect the result to equal the unique number of times a specific city shows up. So in this example the resource from Cosmos should be [3].

Running this query in Azure Portal's Data Explorer the result is [3] as expected. When I run the same query using the REST API, the result is [0]. The actual resource returned from the API is [0, 3].

How I'm using the API:

  let result = await cosmosDb.container(containerName)
    .items.query({
      query: query,
    })
    .fetchAll()
  return result.resources[0] // returns 0 and the result is [0, 3]

I've tried a few combinations of COUNT / DISTINCT but I get the same discrepancy between API and Data Explorer.

Any ideas what I am doing wrong and why results are different from the REST API vs Data Explorer?

Thanks in advance.

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
576 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,901 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-06-24T06:13:34.8433333+00:00

    It can happen as a result of pagination in REST API. You need to handle pagination correctly to retrieve all the results. Here's an example of how you can modify your code to handle pagination:

    let result = await cosmosDb.container(containerName)
      .items.query({
        query: query,
      })
      .fetchAll();
    
    let totalCount = 0;
    for (const page of result.byPage()) {
      const response = await page.fetchNext();
      const resources = response.resources;
      totalCount += resources.length;
    }
    
    return totalCount;
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.