批量更新DocumentDB中的数据 [英] Bulk updating data in DocumentDB

查看:52
本文介绍了批量更新DocumentDB中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一个默认值的属性添加到我通过SELECT查询检索的一组文档中(如果它们不包含任何值).

I have a desire to add a property with a default value to a set of documents that I retrieve via a SELECT query if they contain no value.

我分两部分考虑了这一点:

I was thinking of this in two parts:

  1. 选择*从c文章中article.details.locale ='en-us'

我想找到不存在article.details.x 的所有文章.

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

  1. 添加属性article.details.x = true

我希望可以通过Azure门户支持该EXEC命令,因此我不必创建迁移工具即可一次运行此命令,但是我在门户中找不到此选项.这可能吗?

I was hoping this EXEC command could be supported via the Azure Portal so I don't have to create a migration tool to run this command once but I couldn't find this option in the portal. Is this possible?

推荐答案

您可以将Azure Document DB Studio用作创建和执行存储过程的前端.可以在此处找到.它很容易设置和使用.

You can use Azure Document DB Studio as a front end to creating and executing a stored procedure. It can be found here. It's pretty easy to setup and use.

我根据您的示例模拟了存储过程:

I've mocked up a stored procedure based on your example:

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");
        }
    }
}

我在此轮廓应该与您需要做的很接近.

This outline should be close to what you need to do.

这篇关于批量更新DocumentDB中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆