Bopsi
Bopsi

Reputation: 2424

rethinkdb delete nested property

I have following table structure -

{
  "id": 1,
  "prop": {
    "1": {
      "bunch-of-stuffs": "foo"
    },
    "2": {
      "bunch-of-stuffs": "bar"
    }
  }
}

I want to remove "prop" with the key "2" so that it looks like:

{
  "id": 1,
  "prop": {
    "1":{
      "bunch-of-stuffs": "foo"
    }
  }
}

I have tried the update, but it doesn't work -

r.table("sad").get(1).update({"prop":{"1":{ "bunch-of-stuffs": "foo" }}})

Upvotes: 1

Views: 199

Answers (1)

taygetos
taygetos

Reputation: 3040

In your particular case you can simply call the .replace function to replace the whole document as you know every value:

r.table("sad").get(1).replace({
 "id":1,
 "prop":{
    "1":{ "bunch-of-stuffs": "foo" }
  }
})

If you know the id of the "prop" that you want to delete, you can use the replace like follows, to avoid sending the whole json document over the wire:

r.table("sad").get(1).replace(function(doc){
      return {"id":1, "prop": doc("prop").without("2")}
})

Upvotes: 0

Related Questions