删除字段并更新文档中的其他字段

时间:2015-05-28 18:56:32

标签: rethinkdb

是否可以从文档中删除字段并在一个查询中更新同一文档中的其他字段?

Afaik,要删除字段,您必须使用替换查询,如下所示:

r.db("db").table("table").get("some-id").replace(r.row.without("field-to-remove"))

要更新:

r.db("db").table("table").get("some-id").update({ "field-to-update": "new-value" })

但将这两者链接在一起并不起作用。运行以下查询时出现“RqlRuntimeError:预期类型SELECTION但发现DATUM”错误(替换/更新的顺序无关紧要):

r.db("db").table("table").get("some-id").replace(r.row.without("field-to-remove")).update({ "field-to-update": "new-value" })

2 个答案:

答案 0 :(得分:3)

尝试:

r.db('db').table('table').get('id').update({
   "field-to-remove": r.literal(),
   "field-to-update": "new-value"
})

您不需要在此使用替换,因为您不关心显式设置其他字段。

答案 1 :(得分:0)

您可以在replace函数内使用without mergereplace

r.table('30514947').get("492a41d2-d7dc-4440-8394-3633ae8ac337")
  .replace(function (row) {
    return row
      .without("remove_field")
      .merge({
        "field-to-update": "hello"
      })
  })
相关问题