Dan
Dan

Reputation: 35

Problem with continuation token when querying from Cosmos DB

I'm facing a problem with continuation when querying items from CosmosDB.

I've already tried the following solution but with no success. I'm only able to query the first 10 results of a page even though I get a token that is not NULL.

The token has a size of 10733 bytes and looks like this.

{"token":"+RID:gtQwAJ9KbavOAAAAAAAAAA==#RT:1#TRC:10#FPP:AggAAAAAAAAAAGoAAAAAKAAAAAAAAAAAAADCBc6AEoAGgAqADoASgAaACoAOgBKABoAKgA6AE4AHgAuAD4ASgAeACoAPgBOAB4ALgA+AE4AHgAqAD4ASgAeAC4APgBOAB4ALgA+AE4AIgA2AEYAFgAmADYARgAaACYAPgBKABYAKgA6AE4AHgAuAD4ATgAeAC4APgBOAB4ALgA+AE4AIgAuAD4ATgAeAC4APgBOACIAMgA+AFIAIgAyAD4AUgAmADIAQgAWACIALgBCABIAIgAyAEIAEgAiADIAQgAOACYANgBKAB4AJgA6AEYAGgAqADoATgAeAC4APgB....etc...etc","range":{"min":"","max":"05C1BF3FB3CFC0"}}

Code looks like this. Function QueryDocuments did not work. Instead I had to use QueryItems.

options = {} 
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 10

 q = client.QueryItems(collection_link, query, options)
    results_1 = q._fetch_function(options)
    #this is a string representing a JSON object
    token = results_1[1]['x-ms-continuation']

    data = list(q._fetch_function({'maxItemCount':10,'enableCrossPartitionQuery':True, 'continuation':token}))

Is there a solution to this? Thanks for your help.

Upvotes: 1

Views: 722

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

Please use pydocumentdb package and refer to below sample code.

from pydocumentdb import document_client

endpoint = "https://***.documents.azure.com:443/";
primaryKey = "***";

client = document_client.DocumentClient(endpoint, {'masterKey': primaryKey})

collection_link = "dbs/db/colls/coll"

query = "select c.id from c"

query_with_optional_parameters = [];

q = client.QueryDocuments(collection_link, query, {'maxItemCount': 2})
results_1 = q._fetch_function({'maxItemCount': 2})

print(results_1)
token = results_1[1]['x-ms-continuation']
results_2 = q._fetch_function({'maxItemCount': 2, 'continuation': token})

print(results_2)

Output:

enter image description here

Upvotes: 2

Related Questions