How to update a nested object in rethinkDB

女生的网名这么多〃 提交于 2019-12-11 06:36:56

问题


I am getting an issue while updating a nested object.sample data like this

{
  "status":{
     "draft":{
           "status":"draft",
           "rating":4
      },
     "review":{
            "status":"review",
           "rating":4
      },
     "publish":{
          "status":"publish",
           "rating":4
      }

    }
}

In the above object some times the draft/ review/ publish are empty objects and need to check the condition and update the rating in the object.I have tried like this but getting error.

query:

     r.db('sample_db').table('table').filter({id:'xxxxxxx'})
  .update({"draft": r.row('status').map(function(data){
     return r.branch(
       data('draft').hasFields({'status':true}),
      data.merge({ "rating": 100 }),
      data
     )})
    })

Error:

 {
    "deleted": 0 ,
    "errors": 1 ,
    "first_error":  "Cannot convert OBJECT to SEQUENCE" ,
    "inserted": 0 ,
    "replaced": 0 ,
    "skipped": 0 ,
    "unchanged": 0
    }

can any one help me to solve this query. Thanks in advance.


回答1:


I am not sure exactly what you are trying to do here. But to update the rating inside the draft element depending whether it contains a status field you do:

r.db('sample_db').table('table').get('idxxxxxxx')
  .update(function(e){
    return r.branch(e('status')('draft').hasFields('status'), 
       {'status':{'draft':{'rating':100}}}, e)
  })


来源:https://stackoverflow.com/questions/51753745/how-to-update-a-nested-object-in-rethinkdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!