javascript - Rethinkdb 通过一个查询和条件进行多次更新

标签 javascript updates rethinkdb

如何使用 JavaScript 通过一个查询和条件执行多次更新?

例如,我的文档:

[
    {
        "Author": "Auto",
        "Number": 5,
        "RandomText": "dddbd",
        "Tag": "Srebro",
        "id": "10fbd309-5a7a-4cd4-ac68-c71d7a336498"
    },
    {
        "Author": "Auto",
        "Number": 8,
        "RandomText": "cccac",
        "Tag": "Srebro",
        "id": "37f9694d-cde8-46bd-8581-e85515f8262f"
    },
    {
        "Author": "Auto",
        "Number": 6,
        "RandomText": "fffaf",
        "Tag": "Srebro",
        "id": "b7559a48-a01a-4f26-89cf-35373bdbb411"
    }
]

这是我的查询:

UpdateIndex()
   {
      this.r.table(this.params.table).update((row) => {
         let result;

         console.log(this.r.expr([row]));

         this.r.branch(
               this.r.row(this.Index2).lt(10),
                       result = "Lucky",
                       result = "Good"
                      );
         /*
         if(this.r.row("Number").lt(3)) result = "Bad";
         else if (this.r.row("Number").lt(5)) result = "Poor";
         else if (this.r.row("Number").lt(10)) result = "Lucky";
         else if (this.r.row("Number").lt(20)) result = "Good";
         else if (this.r.row("Number").lt(50)) result = "Great";
         else result = "Mystic";
         */

         console.log(result);

         return this.r.object(this.Index2, result);
      }).run(this.conn, this.CheckResult.bind(this));
  }

我为什么要这样做?我创建了第二个索引 (this.Index2 = 'Opinion'),现在我想用我的条件描述的值填充此索引。 但每个文档都有相同的值(例如:Bad)。如何更新文档,但为每个文档运行条件并使用一个查询?

最佳答案

分配给这样的局部变量(在您的情况下是result)与RethinkDB驱动程序构建查询对象以发送到服务器的方式不兼容。当您编写如上所述的代码时,您将在客户端上的局部变量中存储一次文字字符串(而不是在服务器上每行存储一次),然后在您返回的查询底部将该文字发送到服务器功能。您也无法按照您尝试的方式使用console.log;它在客户端上运行,但您的查询在服务器上执行。您可能会发现http://rethinkdb.com/blog/lambda-functions/对于理解客户端如何使用您传递给诸如 update 之类的命令的匿名函数非常有用。

您应该使用 do 来进行变量绑定(bind):

r.table(params.table).update(function(row) {
  return r.branch(r.row(Index2).lt(10), "Lucky", "Good").do(function(res) {
    return r.object(Index2, res);
  });
})

关于javascript - Rethinkdb 通过一个查询和条件进行多次更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31028892/

相关文章:

javascript - 在点击处理程序中获取元素属性

javascript - 我可以用 bluebird Promises 提前打破链条吗?

jsf - 了解 PrimeFaces 流程/更新和 JSF f :ajax execute/render attributes

Rethinkdb 2.2 带有 include_initial 的变更源

go - 为什么 RethinkDB 很慢?

node.js - 匹配 RethinkDB 中给定数据数组的至少一个元素

javascript - 我今天遇到了 window.addEventListener 和 React Native 的问题

javascript - ajax调用后单选按钮不改变(knockout js)

ios - 更新 RealmSwift 中的对象?

updates - 如何卸载 Visual Studio 2017 的最新更新?