2

Using the V2 Javascript SDK for CosmosDb, I'm getting the TOP 10 items from the db:

var query = "SELECT TOP 10 * FROM People"

const querySpec = {
    query: query
};

var result = await container.items.query(querySpec).toArray();

How do I get the continuation token for the next 10 results and how do I set it in my next query?

Emre Kenci
  • 2,715
  • 4
  • 26
  • 35

1 Answers1

3

Emre. Please refer to my working sample code:

const cosmos = require('@azure/cosmos');
const CosmosClient = cosmos.CosmosClient;

const endpoint = "https://***.documents.azure.com:443/";                 // Add your endpoint
const masterKey = "***";  // Add the masterkey of the endpoint
const client = new CosmosClient({ endpoint, auth: { masterKey } });
const databaseId = "db";
const containerId = "coll";

async function run() {
    const { container, database } = await init();
    const querySpec = {
        query: "SELECT r.id,r._ts FROM root r"
    };
    const queryOptions  = {
        maxItemCount : 1
    }
   const queryIterator = await container.items.query(querySpec,queryOptions);
    while (queryIterator.hasMoreResults()) {
        const { result: results, headers } = await queryIterator.executeNext();
        console.log(results)
        console.log(headers)

        if (results === undefined) {
            // no more results
            break;
        }   
    }
}

async function init() {
    const { database } = await client.databases.createIfNotExists({ id: databaseId });
    const { container } = await database.containers.createIfNotExists({ id: containerId });
    return { database, container };
}

run().catch(err => {
    console.error(err);
});

You could find the continuation token in the console.log(headers).

enter image description here

More details ,please refer to the source code.


Update Answer:

Please refer to my sample function:

    async function queryItems1(continuationToken) {
    const { container, database } = await init();
    const querySpec = {
        query: "SELECT r.id,r._ts FROM root r"
    };
    const queryOptions  = {
        maxItemCount : 2,
        continuation : continuationToken
    };

    const queryIterator = await container.items.query(querySpec,queryOptions);
    if (queryIterator.hasMoreResults()) {
        const { result: results, headers } = await queryIterator.executeNext();
        console.log(results)
        const token = headers['x-ms-continuation'];
        if(token){
            await queryItems1(token);
        }       
    }   
}
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Thanks. I was looking at the sample but the piece you posted here isn't on that file. I need to be able to send the continuation token to the client and then SET it on the next request. How do I do that? I need to be able to do exactly what is shown here for google datastore https://github.com/googleapis/nodejs-datastore/blob/595d988fa8bc61c0925a14a7b7aaabb1e9ac1cec/samples/concepts.js#L967 – Emre Kenci Sep 04 '18 at 07:30
  • @EmreKenci Please see my update answer. I tested it to log data successfully. – Jay Gong Sep 04 '18 at 09:09
  • Perfect. Thank you, Jay. – Emre Kenci Sep 04 '18 at 10:41