Back to top

    ReQL command: merge

    Command syntax

    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.


    Example: Equip every hero for battle, using a subquery function to retrieve their weapons.

    r.table('marvel').merge{ |hero|
        { :weapons => r.table('weapons').get(hero['weapon_id']) }

    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{ |post|
        { :comments => r.table('comments').get_all(post['id'],
            {:index => 'post_id'}).coerce_to('array') }

    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}}}

    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}})}

    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()}}

    Get more help

    Couldn't find what you were looking for?