1

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.

Community
  • 1
  • 1
fpluis
  • 189
  • 1
  • 13
  • 1
    I'm having some trouble understanding your example. The objects in `some_array` appear to have two fields, both of which have `id`s, and the syntax for `properties` is incorrect (I can't figure out if it's supposed to be an array or an object). – mlucy Sep 13 '16 at 05:31
  • @mlucy Thanks, it was indeed a mistake. Properties is an array of objects, and I want to update one of those objects by id. – fpluis Sep 13 '16 at 07:58

1 Answers1

0

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);
    })}};
  })};
})
mlucy
  • 5,249
  • 1
  • 17
  • 21
  • I have tested this code, and the count is not increased. Besides, it is not filtering at any point to update only a specific some_array object, but is instead trying to update all of them. Sorry it took me so long to respond to this answer. – fpluis Sep 17 '16 at 15:37