DocumentDB:无法更新所有记录 - 一次仅更新 100 条记录
DocumentDB: Unable to update ALL records - Only 100 records are updated at a time
在我的文档中,当很少的字段(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 前提条件,因为如果文档在脚本启动后被修改,更新文档将因快照隔离而失败。
在我的文档中,当很少的字段(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 前提条件,因为如果文档在脚本启动后被修改,更新文档将因快照隔离而失败。