0

I am planning to update the document, if similar values already exists in other records of the document.

Say, if root document has few records in which few fields(track,time) are similar, then I want to update the isOriginal field for such record as false.

function updateArticlesDetailsX() {

   var collection = getContext().getCollection();
   var collectionLink = collection.getSelfLink();
   var response = getContext().getResponse();
   var docCount = 0;
   var counter = 0;

   tryQueryAndUpdate();

   function tryQueryAndUpdate(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,
                            function queryCallback(err, documents, responseOptions) {
                                     //response.setBody(responseOptions);

                                     if (err) throw err;
                                     if (documents.length > 0) {
                                         document.upd
                                        // If at least one document is found, update it.
                                        docCount = documents.length;
                                        //response.setBody("Found " + docCount + " documents");
                                        for (var i=0; i<docCount; i++){
                                            tryUpdate(documents[i]);
                                        }
                                        //response.setBody("Updated " + docCount + " documents");
                                      }
                                      if (responseOptions.continuation) {
                                          // Else if the query came back empty, but with a continuation token;
                                          // repeat the query w/ the token.
                                        tryQueryAndUpdate(responseOptions.continuation);
                                      }
                                      //else {
                                        //     throw new Error("Document not found.");
                                          //   }
                            });

        if (!isAccepted) {
            //throw new Error("The stored procedure timed out");
        }
    }

    function tryUpdate(document) {
        //Optimistic concurrency control via HTTP ETag.
        var requestOptions = { etag: document._etag };

        //Update statement goes here:
        document.update({"track":{document.track},"Time":{document.Time} }, {"$set":{"isOriginal":"false!"}});
        document.created = {
      date: "2016-06-22 19:18:14",
      epoch: 1466623094582
    }
//response.setBody(document);

        var isAccepted = collection
                         .replaceDocument(document._self,
                                          document,
                                          requestOptions,
                                          function replaceCallback(err, updatedDocument, responseOptions) {
                                                   if (err) throw err;
                                                   counter++;
                                                   response.setBody("Updated " + counter + " documents");
                                                // response.setBody(updatedDocument);
                                           });

        // If we hit execution bounds - throw an exception.
        if (!isAccepted) {
            //throw new Error("The stored procedure timed out");
        }
    }
}
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • "other records of the document" does not compute. Do you mean "other fields of the document"? If I understand this correctly, you want to update an existing document, correct? If so, DocumentDB doesn't have an incremental update in place. You have to retrieve the document, change the fields you want to change, then save the entire thing. Does that help? – Larry Maccherone Jul 07 '16 at 15:04
  • So, if i have a records with values (id:1,time:10, Track:B, IsOriginal:True) and other record has values (id:2,time:10, Track:B, IsOriginal:True) i want to update (isOriginal to false for id:2) as id: 1 has same time and track values – TheDeveloper Jul 07 '16 at 16:29
  • Still not following you. If you want to change id:2, why talk about id:1? Are you possibly thinking of a flyweight pattern which conserves space? – Larry Maccherone Jul 07 '16 at 19:12
  • Well, I want to change id2, only if the id:2 values are similar to id:1 – TheDeveloper Jul 08 '16 at 14:02

0 Answers0