Share via


QueryIterator class

Represents a QueryIterator Object, an implementation of feed or query response that enables traversal and iterating over the response in the Azure Cosmos DB database service.

Methods

fetchAll()

Fetch all pages for the query and return a single FeedResponse.

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resources } = await container.items
  .query("SELECT * from c WHERE c.isCapitol = true")
  .fetchAll();
fetchNext()

Retrieve the next batch from the feed.

This may or may not fetch more pages from the backend depending on your settings and the type of query. Aggregate queries will generally fetch all backend pages before returning the first batch of responses.

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec = {
  query: "SELECT c.status, COUNT(c.id) AS count FROM c GROUP BY c.status",
};
const queryOptions = {
  maxItemCount: 10, // maximum number of items to return per page
  enableCrossPartitionQuery: true,
};
const queryIterator = container.items.query(querySpec, queryOptions);
while (queryIterator.hasMoreResults()) {
  const { resources: result } = await queryIterator.fetchNext();
  // process results
}
getAsyncIterator()

Gets an async iterator that will yield results until completion.

NOTE: AsyncIterators are a very new feature and you might need to use polyfils/etc. in order to use them in your code.

If you're using TypeScript, you can use the following polyfill as long as you target ES6 or higher and are running on Node 6 or higher.

if (!Symbol || !Symbol.asyncIterator) {
  (Symbol as any).asyncIterator = Symbol.for("Symbol.asyncIterator");
}

Example

Iterate over all databases

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

for await (const { resources: db } of client.databases.readAll().getAsyncIterator()) {
  console.log(`Got ${db} from AsyncIterator`);
}
hasMoreResults()

Determine if there are still remaining resources to process based on the value of the continuation token or the elements remaining on the current batch in the QueryIterator.

reset()

Reset the QueryIterator to the beginning and clear all the resources inside it

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: "Test Database" });
const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec = {
  query: "SELECT c.status, COUNT(c.id) AS count FROM c GROUP BY c.status",
};
const queryIterator = container.items.query(querySpec);
while (queryIterator.hasMoreResults()) {
  const { resources: result } = await queryIterator.fetchNext();
  // process results
}
queryIterator.reset();

Method Details

fetchAll()

Fetch all pages for the query and return a single FeedResponse.

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resources } = await container.items
  .query("SELECT * from c WHERE c.isCapitol = true")
  .fetchAll();
function fetchAll(): Promise<FeedResponse<T>>

Returns

Promise<FeedResponse<T>>

fetchNext()

Retrieve the next batch from the feed.

This may or may not fetch more pages from the backend depending on your settings and the type of query. Aggregate queries will generally fetch all backend pages before returning the first batch of responses.

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec = {
  query: "SELECT c.status, COUNT(c.id) AS count FROM c GROUP BY c.status",
};
const queryOptions = {
  maxItemCount: 10, // maximum number of items to return per page
  enableCrossPartitionQuery: true,
};
const queryIterator = container.items.query(querySpec, queryOptions);
while (queryIterator.hasMoreResults()) {
  const { resources: result } = await queryIterator.fetchNext();
  // process results
}
function fetchNext(): Promise<FeedResponse<T>>

Returns

Promise<FeedResponse<T>>

getAsyncIterator()

Gets an async iterator that will yield results until completion.

NOTE: AsyncIterators are a very new feature and you might need to use polyfils/etc. in order to use them in your code.

If you're using TypeScript, you can use the following polyfill as long as you target ES6 or higher and are running on Node 6 or higher.

if (!Symbol || !Symbol.asyncIterator) {
  (Symbol as any).asyncIterator = Symbol.for("Symbol.asyncIterator");
}

Example

Iterate over all databases

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

for await (const { resources: db } of client.databases.readAll().getAsyncIterator()) {
  console.log(`Got ${db} from AsyncIterator`);
}
function getAsyncIterator(): AsyncIterable<FeedResponse<T>>

Returns

AsyncIterable<FeedResponse<T>>

hasMoreResults()

Determine if there are still remaining resources to process based on the value of the continuation token or the elements remaining on the current batch in the QueryIterator.

function hasMoreResults(): boolean

Returns

boolean

true if there is other elements to process in the QueryIterator.

reset()

Reset the QueryIterator to the beginning and clear all the resources inside it

Example

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: "Test Database" });
const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec = {
  query: "SELECT c.status, COUNT(c.id) AS count FROM c GROUP BY c.status",
};
const queryIterator = container.items.query(querySpec);
while (queryIterator.hasMoreResults()) {
  const { resources: result } = await queryIterator.fetchNext();
  // process results
}
queryIterator.reset();
function reset()