GlurG
GlurG

Reputation: 237

Removing a field and updating another field in a document

Is it possible to remove a field from a document and update another field in the same document in one query?

Afaik, to remove field, you have to use a replace query, like so:

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

And to update:

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

But chaining these two together doesn't work. I get a "RqlRuntimeError: Expected type SELECTION but found DATUM" error when running the following query (the order of the replace/update doesn't matter):

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

Upvotes: 2

Views: 916

Answers (2)

Jorge Silva
Jorge Silva

Reputation: 4614

You can use replace with without and merge inside of your replace function:

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

Upvotes: 0

deontologician
deontologician

Reputation: 2814

Try:

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

You don't need to use replace here since you don't care about explicitly setting the other fields.

Upvotes: 3

Related Questions