gpt4 book ai didi

azure - DocumentDB,如何在 SP 中使用 continuationToken

转载 作者:行者123 更新时间:2023-12-03 04:29:47 24 4
gpt4 key购买 nike

下一个 SP 假设运行该集合并保留对下一批文档的查询(每批 10 个文档)。但每次都会返回相同的 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);
}}

最佳答案

在您编写的脚本中,查询的执行是同步完成的,并且它们使用相同的初始延续标记(为 null)排队。相反,我们需要从第一个查询中获取 token ,然后对下一个查询进行排队并继续。

下面的示例应该有助于实现您正在寻找的内容

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);
}
}

关于azure - DocumentDB,如何在 SP 中使用 continuationToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com