我在更新嵌套对象时遇到问题。像这样的示例数据
{
"status":{
"draft":{
"status":"draft",
"rating":4
},
"review":{
"status":"review",
"rating":4
},
"publish":{
"status":"publish",
"rating":4
}
}
}
在上述对象中,有时草稿/审核/发布是空对象,需要检查条件并更新对象中的评级。我已经尝试过这样的操作,但出现错误。
查询:
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
)})
})
错误:
{
"deleted": 0 ,
"errors": 1 ,
"first_error": "Cannot convert OBJECT to SEQUENCE" ,
"inserted": 0 ,
"replaced": 0 ,
"skipped": 0 ,
"unchanged": 0
}
谁能帮我解决这个问题。提前致谢。
最佳答案
我不确定你到底想在这里做什么。但要根据草稿元素是否包含状态字段来更新草稿元素内的评级,请执行以下操作:
r.db('sample_db').table('table').get('idxxxxxxx')
.update(function(e){
return r.branch(e('status')('draft').hasFields('status'),
{'status':{'draft':{'rating':100}}}, e)
})
关于node.js - 如何更新 rethinkDB 中的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753745/