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

时间:2016-07-13 02:29:22

标签: azure azure-cosmosdb

在我的文档中,当我想关闭批准标记时,几个字段(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()中收到错误。我无法理解我收到错误的原因和原因。

它说:

  

错误:{&#34;错误&#34;:[&#34;未达到指定的前提条件之一&#34;]}

1 个答案:

答案 0 :(得分:1)

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

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

BTW,对于存储过程,不需要_etag前置条件,因为如果在脚本启​​动后修改了文档,更新文档将因快照隔离而失败。