singleSelection.merge([object | function, object | function, ...]) → object object.merge([object | function, object | function, ...]) → object sequence.merge([object | function, object | function, ...]) → stream array.merge([object | function, object | function, ...]) → array
Merge two or more objects together to construct a new object with properties from all. When there is a conflict between field names, preference is given to fields in the rightmost object in the argument list merge
also accepts a subquery function that returns an object, which will be used similarly to a map function.
Example: Equip Thor for battle.
r.table('marvel').get('thor').merge( r.table('equipment').get('hammer'), r.table('equipment').get('pimento_sandwich') ).run(conn)
Example: Equip every hero for battle, using a subquery function to retrieve their weapons.
r.table('marvel').merge(lambda hero: { 'weapons': r.table('weapons').get(hero['weapon_id']) } ).run(conn)
Example: Use merge
to join each blog post with its comments.
Note that the sequence being merged—in this example, the comments—must be coerced from a selection to an array. Without coerce_to
the operation will throw an error (“Expected type DATUM but found SELECTION”).
r.table('posts').merge(lambda post: { 'comments': r.table('comments').get_all(post['id'], index='post_id').coerce_to('array') } ).run(conn)
Example: Merge can be used recursively to modify object within objects.
r.expr({'weapons' : {'spectacular graviton beam' : {'dmg' : 10, 'cooldown' : 20}}}).merge( {'weapons' : {'spectacular graviton beam' : {'dmg' : 10}}} ).run(conn)
Example: To replace a nested object with another object you can use the literal keyword.
r.expr({'weapons' : {'spectacular graviton beam' : {'dmg' : 10, 'cooldown' : 20}}}).merge( {'weapons' : r.literal({'repulsor rays' : {'dmg' : 3, 'cooldown' : 0}})} ).run(conn)
Example: Literal can be used to remove keys from an object as well.
r.expr({'weapons' : {'spectacular graviton beam' : {'dmg' : 10, 'cooldown' : 20}}}).merge( {'weapons' : {'spectacular graviton beam' : r.literal()}} ).run(conn)
Couldn't find what you were looking for?
© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/python/merge/