Azure DocumentDB,如何在SP中使用continuationToken
假设下一个SP运行集合并保留对下一批文档(每批10个文档)的查询。但每次都返回相同的10个文档Azure DocumentDB,如何在SP中使用continuationToken,azure,stored-procedures,azure-cosmosdb,Azure,Stored Procedures,Azure Cosmosdb,假设下一个SP运行集合并保留对下一批文档(每批10个文档)的查询。但每次都返回相同的10个文档 function sample(prefix) { var continuations = [], ids = [], context = getContext(), collection = context.getCollection(), response = context.getResponse(); var queryOptions = { pageSize: 10,
function sample(prefix) {
var continuations = [],
ids = [],
context = getContext(),
collection = context.getCollection(),
response = context.getResponse();
var queryOptions = { pageSize: 10, continuation: null };
for (i = 0; i < 10; i++) {
// get all user wish list actions
var query = "select * from w",
accept = collection.queryDocuments(collection.getSelfLink(), query, queryOptions, processMultiUsers);
if (!accept) throw "Unable to read user's sessions";
}
getContext().getResponse().setBody(ids);
function processMultiUsers(err, docs, options) {
if (err) throw new Error("Error: " + err.message);
if (docs == undefined || docs.length == 0) throw new Error("Warning: Users not exists");
for (j = 0; j < docs.length; j++) {
ids.push(docs[j].UserId);
}
queryOptions.continuation = options.continuation;
continuations.push(options.continuation);
}}
函数示例(前缀){
var continuations=[],
ids=[],
context=getContext(),
collection=context.getCollection(),
response=context.getResponse();
var queryOptions={pageSize:10,continuation:null};
对于(i=0;i<10;i++){
//获取所有用户愿望列表操作
var query=“选择*来自w”,
accept=collection.queryDocuments(collection.getSelfLink(),query,queryOptions,processMultiUsers);
如果(!accept)抛出“无法读取用户会话”;
}
getContext().getResponse().setBody(ID);
函数processMultiUsers(错误、文档、选项){
如果(错误)抛出新错误(“错误:+err.message”);
如果(docs==undefined | | docs.length==0)抛出新错误(“警告:用户不存在”);
对于(j=0;j
在您编写的脚本中,查询的执行是同步完成的,并且它们使用相同的初始延续令牌(null)排队。相反,我们需要从第一个查询中获取令牌,然后将下一个查询排队并继续
下面的示例应该有助于实现您所寻找的目标
function sample(continuationToken) {
var collection = getContext().getCollection();
var maxResult = 10;
var documentsProcessed = 0;
var ids = [];
var filterQuery = "select * from w";
tryQuery(continuationToken);
function tryQuery(nextContinuationToken) {
var responseOptions = { continuation: nextContinuationToken, pageSize: maxResult };
if (documentsProcessed >= maxResult || !query(responseOptions)) {
setBody(nextContinuationToken);
}
}
function query(responseOptions) {
return (filterQuery && filterQuery.length) ?
collection.queryDocuments(collection.getSelfLink(), filterQuery, responseOptions, onReadDocuments) :
collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments);
}
function onReadDocuments(err, docFeed, responseOptions) {
if (err) {
throw 'Error while reading document: ' + err;
}
documentsProcessed += docFeed.length;
for (var i = 0; i < documentsProcessed; i++) {
ids.push(docFeed[i].UserId);
}
if (responseOptions.continuation) {
tryQuery(responseOptions.continuation);
} else {
setBody(null);
}
}
function setBody(continuationToken) {
var body = { continuationToken: continuationToken, documentsProcessed: documentsProcessed, ids: ids };
getContext().getResponse().setBody(body);
}
}
函数示例(continuationToken){
var collection=getContext().getCollection();
var maxResult=10;
var documentsProcessed=0;
var-id=[];
var filterQuery=“选择*来自w”;
tryQuery(continuationToken);
函数tryQuery(nextContinuationToken){
var responseOptions={continuation:nextContinuationToken,pageSize:maxResult};
如果(documentsProcessed>=maxResult | |!查询(响应选项)){
setBody(下一个ContinuationToken);
}
}
函数查询(响应选项){
返回(filterQuery&&filterQuery.length)?
collection.queryDocuments(collection.getSelfLink()、filterQuery、responseOptions、onReadDocuments):
collection.readDocuments(collection.getSelfLink()、responseOptions、onReadDocuments);
}
函数onReadDocuments(err、docFeed、responseOptions){
如果(错误){
读取文档时抛出“错误:”+错误;
}
documentsProcessed+=docFeed.length;
对于(变量i=0;i
在您编写的脚本中,查询的执行是同步完成的,并且它们使用相同的初始延续令牌(null)排队。相反,我们需要从第一个查询中获取令牌,然后将下一个查询排队并继续
下面的示例应该有助于实现您所寻找的目标
function sample(continuationToken) {
var collection = getContext().getCollection();
var maxResult = 10;
var documentsProcessed = 0;
var ids = [];
var filterQuery = "select * from w";
tryQuery(continuationToken);
function tryQuery(nextContinuationToken) {
var responseOptions = { continuation: nextContinuationToken, pageSize: maxResult };
if (documentsProcessed >= maxResult || !query(responseOptions)) {
setBody(nextContinuationToken);
}
}
function query(responseOptions) {
return (filterQuery && filterQuery.length) ?
collection.queryDocuments(collection.getSelfLink(), filterQuery, responseOptions, onReadDocuments) :
collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments);
}
function onReadDocuments(err, docFeed, responseOptions) {
if (err) {
throw 'Error while reading document: ' + err;
}
documentsProcessed += docFeed.length;
for (var i = 0; i < documentsProcessed; i++) {
ids.push(docFeed[i].UserId);
}
if (responseOptions.continuation) {
tryQuery(responseOptions.continuation);
} else {
setBody(null);
}
}
function setBody(continuationToken) {
var body = { continuationToken: continuationToken, documentsProcessed: documentsProcessed, ids: ids };
getContext().getResponse().setBody(body);
}
}
函数示例(continuationToken){
var collection=getContext().getCollection();
var maxResult=10;
var documentsProcessed=0;
var-id=[];
var filterQuery=“选择*来自w”;
tryQuery(continuationToken);
函数tryQuery(nextContinuationToken){
var responseOptions={continuation:nextContinuationToken,pageSize:maxResult};
如果(documentsProcessed>=maxResult | |!查询(响应选项)){
setBody(下一个ContinuationToken);
}
}
函数查询(响应选项){
返回(filterQuery&&filterQuery.length)?
collection.queryDocuments(collection.getSelfLink()、filterQuery、responseOptions、onReadDocuments):
collection.readDocuments(collection.getSelfLink()、responseOptions、onReadDocuments);
}
函数onReadDocuments(err、docFeed、responseOptions){
如果(错误){
读取文档时抛出“错误:”+错误;
}
documentsProcessed+=docFeed.length;
对于(变量i=0;i
看一看我刚才写给你的计数问题的答案,看看这是否回答了你的问题。或者,你可以研究这是如何工作的:或者回到这里,问其他问题中没有回答的任何问题,我很乐意帮助你。:-)看一看我刚才写给你的计数问题的答案,看看这是否回答了你的问题。或者,你可以研究这是如何工作的:或者回到这里,问其他问题中没有回答的任何问题,我很乐意帮助你。:-)谢谢你@Shireesh,这就行了。虽然我不明白为什么要把同一个变量“maxResult”放在两个不同的东西上。一个是我要提取的每个请求的大小,另一个是我要提取的文档总数的限制。@GuyAssaf-maxResult主要用于控制最终响应中的总体文档数,但也可以用于将其设置为页面大小,以作为每次往返请求的最大结果。当您将pageSize设置为“x”时,承诺往返将获得atmost(您的意思是,如果我要处理的文档数量有限制,那么最好请求页面大小为w)