【问题标题】:Conditional Delete in RethinkDBRethinkDB 中的条件删除
【发布时间】:2016-09-03 15:09:55
【问题描述】:

我想删除 RethinkDB 中的一个对象,但前提是满足条件。

我想出了以下利用 replace 和 null 的函数(当 null 传递给 replace() 时,RethinkDB 会删除对象)。但是,我无法让它工作,因为 RethinkDB 不断返回 Cannot perform bracket on a non-object non-sequencenull. 我只是想替换一个对象。怎么了?

r.db('test')
  .table('test')
  .get('123')
  .replace(function(thing) {
    return r.branch(thing('color').ne('green'),
      r.error('Object color must be green to be deleted'),
      null)
  }, { returnChanges: true })

【问题讨论】:

    标签: rethinkdb


    【解决方案1】:

    您确定第 123 行存在吗?无论如何,如果您有兴趣避免此错误,有几种方法可以做到这一点,例如为键 color 提供默认值:

        r.db('test')
          .table('test')
          .get("123")
          .replace(function(thing) {
            return r.branch(thing('color').default({color: null}).ne('green'),
              r.error('Object color must be green to be deleted'),
              null)
          }, { returnChanges: true })
    

    【讨论】:

      【解决方案2】:

      我认为问题在于 Kludge 指出您从 get("123") 得到 null。您可以使用 thing.eq(null) 在分支中测试此条件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-11
        • 2023-03-27
        相关资源
        最近更新 更多