2016-07-13 12 views
1

私の文書では、承認フラグをオフにしたいフィールド(投稿、価格、desc)はほとんどありません。DocumentDB:すべてのレコードを更新できません - 一度に100レコードしか更新されません。

この問題では、まず文書内のすべてのレコードのフラグをオフにしてから、一意レコードのみのフラグをオンにします。

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"]}

答えて

1

アズハル、私がここで失敗したものを推測replaceDocumentコールがあります。 _etagの前提条件が満たされていないため、このスクリプト(トランザクション)が開始された後に他の人が文書を変更したために失敗する必要があります。これは可能なケースですか?

スクリプト内で修正されたドキュメントを処理するには、クライアントから再試行する必要があります。あなたが行うことができるのは、(1)カスタムメッセージで例外をスローする(スクリプトトランザクションを中止したい場合)か、(2)リクエスト本体にカスタムフラグを設定するか(スクリプトアクションを実行してコミットする必要がある場合) ExecuteStoredProcedureコールを再試行します。

ところで、ストアドプロシージャの場合、スクリプトの開始後にドキュメントが変更された場合、スナップショットの分離によってドキュメントの更新が失敗するため、_etagの事前条件は必要ありません。

関連する問題