Search code examples
rethinkdbrethinkdb-javascript

append to nested array in rethinkdb


I have a data structure as follows:

{
    id: blah-blah-blah, 
    settings: { 
                stuff: {},
                other_stuff: {},
                provided: []
    },
    ....
}

I need to update various items with dynamically generated keys and/or values. Updating the nested objects is easy. For instance, updating some key inside settings.stuff I can create an update object as follows:

update_object = r.object('settings', r.object('stuff', r.object(key, value))) 

and then run

r.db(foo).table(bar).get(woot).update(update_object).run()

But appending some dynamically generated value to the nested array is beyond me. I gather it's something along the lines of:

r.db(foo).table(bar).get(woot).update(function(document) {
   return {"settings": document("settings")....append(value)

but I'm at a loss about how to fill in the blank in the last line.


Solution

  • OK, I figured it out:

    r.db(foo).table(bar).get(woot)
        .update({'settings': {'provided': r.row('settings')('provided').append(value)}});