如果 RethinkDB 中存在,则更新随机选择的项目

Update randomly selected item if it exists in RethinkDB

我想从 0 到多个项目的集合中随机 select 单个项目,如果存在,则更新特定字段。如果该项目不存在,我希望该功能不执行更新并且 return null.

我当前的 REQL 代码:

r.db('test').table('test')
.filter({
    something: true
}).sample(1).nth(0).default(null).update(function(thing) {
    return r.branch(
        thing.ne(null),
        thing.without('reserve'),
        null
    )
}, {
    returnChanges: true
});

这总是 return 错误:Expected type SELECTION but found DATUM 我不确定如何使用 REQL 解决这个问题。

你可能想这样写:

r.db('test').table('test').filter({something: true}).sample(1).replace(function(thing) {
    return thing.without('reserve');
}, {returnChanges: true});

这将返回一个写入摘要对象,您可以使用它来确定是否实际发生了替换。