Bulk data refresh in DocumentDB

I have a desire to add a property with a default value to the set of documents that I get with a SELECT query if they do not contain a value.

I thought about this in two parts:

  • SELECT * FROM c article WHERE article.details.locale = 'en-us'

I would like to find all articles where article.details.x does not exist.

  1. Add property article.details.x = true

I was hoping that this EXEC command could be supported through the Azure Portal, so I did not need to create a migration tool to run this command once, but I could not find this option on the portal. Is it possible?

+4
source share
2 answers

Azure Document DB Studio . . .

:

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 where IS_DEFINED(r.details.x) != true"
        };

        var requestOptions = {
            continuation: continuation
        };

        var isAccepted =
            collection
            .queryDocuments(collectionLink,
                            query,
                            requestOptions,
                            function queryCallback(err, documents, responseOptions) {
                                     if (err) throw err;
                                     if (documents.length > 0) {
                                        // If at least one document is found, update it.
                                        docCount = documents.length;
                                        for (var i=0; i<docCount; i++){
                                            tryUpdate(documents[i]);
                                        }
                                        response.setBody("Updated " + docCount + " documents");
                                      }
                                      else 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.details.x = "some new value";

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

        // If we hit execution bounds - throw an exception.
        if (!isAccepted) {
            throw new Error("The stored procedure timed out");
        }
    }
}

GitHub.

, .

+5

DocumentDB . Script, . sproc, replaceDocument , . , , , DocumentDB sprocs 5 ( ). , , , sproc , , 5 . IS_DEFINED (collection.field.subfield)!= True (thanks @cnaegle) , , ( ), sproc , .

sproc, DocumentDB Data Migration. Excel, Script, . .

+5

Source: https://habr.com/ru/post/1623983/


All Articles