DocumentDB:无法更新所有记录 - 一次只更新 100 条记录



在我的文档中,当几个字段(帖子,价格,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");
        }
    }
}

我在启用唯一记录()中收到错误。我无法理解我收到错误的原因和原因。

它说:

错误: {"错误":["不满足指定的前提条件之一"]}

爱资哈尔,我想这里失败的是替换文档调用。它一定失败了,因为不满足_etag前提条件,这应该是因为在此脚本(事务)启动后,其他人修改了文档。这是可能的情况吗?

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

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

最新更新