首页 文章

rethinkdb删除嵌套属性

提问于
浏览
1

我有以下表格结构 -

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

我想用键“2”删除“prop”,使它看起来像:

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

我已经尝试过更新,但它不起作用 -

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

1 回答

  • 0

    在您的特定情况下,您可以简单地调用 .replace 函数来替换整个文档,因为您知道每个值:

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

    如果你知道你要删除的“prop”的id,你可以使用如下的替换,以避免通过网络发送整个json文档:

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

相关问题