删除一个字段并更新文档中的另一个字段

Removing a field and updating another field in a document

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

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: Expected type SELECTION but found DATUM" 错误(replace/update 的顺序无关紧要):

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

尝试:

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

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

您可以在 replace 函数中将 replacewithoutmerge 一起使用:

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