How to remove an item from embedded list?
-
23-12-2019 - |
Question
I have a josn named "update",and it has an embedded list "comments" like this:
{
id: "update/0",
//comments contains elements with type:comment
comments: [{
id:"comment/0"
children:[{
id:"comment/00",
children[...]
}]
}]
}
Questions are:
1, How to remove an item from update's field:comments by ids "update/0" and
"comment/0"?
2, How to remove an item from comment's field:children by ids "update/0","
comment/0" and "comment/00"?
Solution
r.table("update").get("update/0").update(function(doc) {
return doc.merge({
comments: doc("comments").concatMap(function(comment) {
return r.branch(
comment("id").eq("comment/0"),
[],
[comment]
)
})
})
})
r.table("update").get("update/0").update(function(doc) {
return doc.merge({
comments: doc("comments").map(function(comment) {
return r.branch(
comment("id").eq("comment/0"),
comment.merge({
children: comment("children").concatMap(function(child) {
return r.branch(
child("id").eq("comment/00"),
[],
[child]
)
})
}),
comment
)
})
})
})
You can also do that with deleteAt
, but you should consider splitting your data in multiple tables
See my comment in How to update an item in a embedded list?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow