How to update a nested object in rethinkDB

2019-07-27 06:50发布

问题:

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)
  })