gpt4 book ai didi

azure - 文档数据库 : Unable to update ALL records - Only 100 records are updated at a time

转载 作者:行者123 更新时间:2023-12-03 03:10:44 25 4
gpt4 key购买 nike

在我的文档中,当几个字段(post、price、desc)时,我想关闭批准标志。

对于这个问题,我首先关闭文档中所有记录的标志,然后仅打开唯一记录的标志。

function SProc() {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var counter = 0;
var responseBody = {
updated: 0,
continuation: true,
error: "",
log: ""
};

// Validate input.
getFullListOfPosts();

// Recursively queries for a document by id w/ support for continuation tokens.
// Calls findDuplicates(document) as soon as the query returns a document.
function getFullListOfPosts(continuation) {
var query = {
query: "select * from root r ORDER BY r.created.epoch DESC"
};

var requestOptions = {
continuation: continuation
};

var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, proccessFullListOfPosts);

// If we hit execution bounds - throw an exception.
if (!isAccepted) {
responseBody.log += "Query not accepted";
response.setBody(responseBody);
throw new Error("The stored procedure timed out");
}
}

function proccessFullListOfPosts(err, documents, responseOptions) {
if (err) {
responseBody.error = err;
throw err;
}
if (documents.length > 0) {
responseBody.log += "Total records: " + documents.length;
var filtered = documents.filter(function(a) {
var key = a.posted_by + '|' + a.price + '|' + a.description + '|' + a.compare_price;
if (!this[key]) {
this[key] = true;
return true;
}
}, Object.create(null))
for (var i = 0; i < documents.length; i++) {
var allRec = documents[i]
disableAllrecords(allRec);
}
for (var i = 0; i < filtered.length; i++) {
var uniqRec = filtered[i]
enableUniqueRecords(uniqRec);
}
}
if (responseOptions.continuation) {
// Else if the query came back empty, but with a continuation token;
// repeat the query w/ the token.
getFullListOfPosts(responseOptions.continuation);
}
}

function disableAllrecords(element) {
element.is_approved = false;
element.likes = 56;
responseBody.log += " Will disable " + element.id + element.is_approved + element.likes;
var requestOptions = {
etag: element._etag
};
var isAccepted = collection.replaceDocument(element._self, element, requestOptions, function(err, updatedDocument, responseOptions) {
if (err) throw err;
counter++;
responseBody.log += " Disabled: " + element.id + " with approval:" + element.is_approved + " Likes:" + element.likes;
});
if (!isAccepted) {
throw new Error("The stored procedure timed out while disabling");
}
}

function enableUniqueRecords(x) {
x.is_approved = true;
x.likes = 65;
responseBody.log += " Will enable " + x.id + x.is_approved + x.likes;
var requestOptions = {
etag: x._etag
};
var isAccepted = collection.replaceDocument(x._self, x, requestOptions, function(err, updatedDocument, responseOptions) {
if (err) throw err;
counter++;
responseBody.log += " Enabled: " + x.id + " with approval:" + x.is_approved + " Likes:" + x.likes;
});
if (!isAccepted) {
throw new Error("The stored procedure timed out while enabling");
}
}
}

我在enableUniqueRecords() 中遇到错误。我无法理解我收到错误的原因和原因。

它说:

Error: {"Errors":["One of the specified pre-condition is not met"]}

最佳答案

Azhar,我想这里失败的是replaceDocument 调用。它一定会失败,因为不满足 _etag 前提条件,这应该是因为在该脚本(事务)启动后文档被其他人修改了。这种情况有可能吗?

要处理脚本内修改的文档,需要从客户端进行重试。您可以做的是(1)使用自定义消息抛出异常(如果您希望中止脚本事务)或(2)在请求正文中设置一些自定义标志(如果您希望到目前为止完成的脚本操作要提交),请识别这一点在客户端上并重试 ExecuteStoredProcedure 调用。

顺便说一句,对于存储过程来说,不需要 _etag 前置条件,因为如果在脚本启​​动后修改了文档,则由于快照隔离,更新文档会失败。

关于azure - 文档数据库 : Unable to update ALL records - Only 100 records are updated at a time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341719/

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