-
-
Notifications
You must be signed in to change notification settings - Fork 164
Description
Let's say I have table posts and every post have an array called comments with objects inside to describe the comment like so comments: [{author: 'Joe', adminComment: true, text: 'Update me'}]
How would you update the text of the comment? I saw that there is an issue for that but the solution is not available yet.
Stack Overflow to the rescue! There is a good explanation for the update query. The problem was that it is with hard-coded index for the element of the array. So here is a full example for updating a single comment that worked for me:
let post_id = 123;
let comment_id = 23434;
let findCommentIndex =
r.table("posts")
.get(post_id)("comments")
.offsetsOf(
r.row("id").match(comment_id)
)
.nth(0)
let update =
findCommentIndex.do((index) => {
return r.table('posts')
.get(post_id)
.update((post) => {
return {
channels: post('comments').changeAt(index,
post('comments')
.nth(index)
.merge({'text': 'updated!'})
)
}
})
})
This operations should be a lot more easier and I hope someday they will be but right now it would be cool if we have some examples like this one on the docs or cookbook because I'm sure I'm not the only one that lost a lot of time to figure this one out.