Reputation: 189
TLDR: The largest problem I am facing is that I don't see how to filter an object inside an array by a key that is nested inside one of its objects.
Assuming a schema with this structure:
{
some_field: "value",
some_array: [
{
item: {
id: "some rethinkdb key",
name: "some object"
},
relevant_field: {
id: "some rethinkdb key",
name: "so relevant",
properties: [
{
id: "target key",
count: 2
},
{
id: "some other rethinkdb key",
count: 1
},
]
}
},
{
item: ...
}
]
}
The problem is the following: I wish to update the 'count' field inside properties for an object inside some_array with id 'target key'. The best way I have found so far is to find the position in 'some_array' of the item I wish to update, and then apply the method found in this question, provided by the accepted answer: How to update embedded document?. I have successfully used that very method on other problems, but not in this one.
EDIT: Fixed the json structure thanks to mlucy's comment.
Upvotes: 1
Views: 253
Reputation: 5289
You can write:
table.update(function(row) {
return {some_array: row('some_array').map(function(el) {
return {relevant_field: {properties: el('relevant_field')('properties').map(function(prop) {
return r.branch(prop('id').eq('target key'), prop.merge({count: prop('count').add(1)}), prop);
})}};
})};
})
Upvotes: 0