Rethinkdb 通过一个查询和条件进行多次更新
Rethinkdb multiple update by one query and conditional
如何使用 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 的驱动程序构建查询对象以发送到服务器的方式。当您像上面那样编写代码时,您将在客户端的局部变量中存储一次文字字符串(而不是在服务器上每行一次),然后在您 return 的查询中将该文字发送到服务器你的功能的底部。您也不能按照您尝试的方式使用 console.log
;在客户端上运行,但您的查询是在服务器上执行的。您可能会发现 http://rethinkdb.com/blog/lambda-functions/ 有助于理解客户端如何处理您传递给 update
.
等命令的匿名函数
您应该使用 do
来代替变量绑定:
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 使用一个查询和条件执行多个更新?
例如我的文档:
[
{
"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 的驱动程序构建查询对象以发送到服务器的方式。当您像上面那样编写代码时,您将在客户端的局部变量中存储一次文字字符串(而不是在服务器上每行一次),然后在您 return 的查询中将该文字发送到服务器你的功能的底部。您也不能按照您尝试的方式使用 console.log
;在客户端上运行,但您的查询是在服务器上执行的。您可能会发现 http://rethinkdb.com/blog/lambda-functions/ 有助于理解客户端如何处理您传递给 update
.
您应该使用 do
来代替变量绑定:
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);
});
})