Merge
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Description
The Merge
function combines two or more objects into one, by merging
the fields and values from both objects into a new object.
The merge starts with a copy of object1
— called result
— and then
merges object2
into result
. If object2
is an array of objects to
merge, each object within object2
is merged into result
in turn.
During the merge, each field of the object to be merged into result
is processed by a resolver function, which determines which value to
use in result
when there is a common field. The default resolver
always decides to use the value from the object to be merged, overriding
the value in result
.
You can provide an optional customResolver
function that overrides the
default resolver, and is used to determine which value to use.
When the resolver returns a null
value, Merge
deletes that field in
result
.
Parameters
Argument | Type | Definition and Requirements |
---|---|---|
|
The first object to merge with the second object. |
|
|
The second object, or array of objects, to merge with the first
object. If an array of objects is provided, each object in the array
is merged with |
|
|
Optional - A lambda function that replaces the default resolver and decides which value to use during the merge. The
where If |
Returns
An object that is the result of merging object1
with object2
(and
any additional objects that you may have provided).
Examples
-
The following query demonstrates a merge of two objects where there is no key conflict:
Value result = await client.Query( Merge( Obj("a", "Apple", "b", "Banana"), Obj("x", "width", "y", "height") ) );
{ "object": { "a": "Apple", "b": "Banana", "x": "width", "y": "height" } }
result, err := client.Query( f.Merge( f.Obj{ "a": "Apple", "b": "Banana", }, f.Obj{ "x": "width", "y": "height", }))
map[a:Apple b:Banana x:width y:height]
System.out.println( client.query( Merge( Obj("a", "Apple", "b", "Banana"), Obj("x", "width", "y", "height") ) ).get());
{a: "Apple", b: "Banana", x: "width", y: "height"}
client.query( q.Merge( { a: 'Apple', b: 'Banana' }, { x: 'width', y: 'height' }, ) ) .then((ret) => console.log(ret))
{ a: 'Apple', b: 'Banana', x: 'width', y: 'height' }
result = client.query( q.merge( { "a": "Apple", "b": "Banana" }, { "x": "width", "y": "height" } ) ) print(result)
{'a': 'Apple', 'b': 'Banana', 'x': 'width', 'y': 'height'}
client.query( Merge( Obj("a" -> "Apple", "b" -> "Banana"), Obj("x" -> "width", "y" -> "height") ) )
{a: "Apple", b: "Banana", x: "width", y: "height"}
-
The following query demonstrates a merge when there is a key conflict and no
resolver
function has been provided:Value result = await client.Query( Merge( Obj("f", "First"), Obj("f", "Fauna") ) );
{ "object": { "f": "Fauna" } }
result, err := client.Query( f.Merge( f.Obj{ "f": "First", }, f.Obj{ "f": "Fauna", }))
map[f:Fauna]
System.out.println( client.query( Merge( Obj("f", "First"), Obj("f", "Fauna") ) ).get());
{f: "Fauna"}
client.query( q.Merge( { f: 'First' }, { f: 'Fauna' }, ) ) .then((ret) => console.log(ret))
{ f: 'Fauna' }
result = client.query( q.merge( { "f": "First" }, { "f": "Fauna" } ) ) print(result)
{'f': 'Fauna'}
client.query( Merge( Obj("f" -> "First"), Obj("f" -> "Fauna") ) )
{f: "Fauna"}
-
The following query demonstrates a merge when there is a key conflict and a custom
resolver
function has been provided:Value result = await client.Query( Merge( Obj("c", "Compare", "d", "Difference"), Obj("c", "Contrast", "d", "Delta"), Lambda( Arr("key", "a", "b"), If( EqualsFn(Var("key"), "c"), Var("a"), Var("b") ) ) ) );
{ "object": { "c": "Compare", "d": "Delta" } }
result, err := client.Query( f.Merge( f.Obj{ "c": "Compare", "d": "Difference" }, f.Obj{ "c": "Contrast", "d": "Delta" }, f.ConflictResolver( f.Lambda( f.Arr{"key", "a", "b"}, f.If( f.Equals(f.Var("key"), "c"), f.Var("a"), f.Var("b"))))))
map[c:Compare d:Delta]
System.out.println( client.query( Merge( Obj("c", "Compare", "d", "Difference"), Obj("c", "Contrast", "d", "Delta"), Lambda( Arr("key", "a", "b"), If( Equals(Var("key"), "c"), Var("a"), Var("b") ) ) ) ).get());
{c: "Compare", d: "Delta"}
client.query( q.Merge( { c: 'Compare', d: 'Difference' }, { c: 'Contrast', d: 'Delta' }, q.Lambda( ['key', 'a', 'b'], q.If( q.Equals(q.Var('key'), 'c'), q.Var('a'), q.Var('b'), ) ) ) ) .then((ret) => console.log(ret))
{ c: 'Compare', d: 'Delta' }
result = client.query( q.merge( { "c": "Compare", "d": "Difference" }, { "c": "Contrast", "d": "Delta" }, q.lambda_( ["key", "a", "b"], q.if_( q.equals(q.var("key"), "c"), q.var("a"), q.var("b") ) ) ) ) print(result)
{'c': 'Compare', 'd': 'Delta'}
println(Await.result( client.query( Merge( Obj("c" -> "Compare", "d" -> "Difference"), Obj("c" -> "Contrast", "d" -> "Delta"), Lambda( Arr("key", "a", "b"), If( Equals(Var("key"), "c"), Var("a"), Var("b") ) ) ) ), 5.seconds ))
{c: "Compare", d: "Delta"}
-
The following query demonstrates a merge when an array of objects is provided:
Value result = await client.Query( Merge( Obj("c", "Compare", "d", "Difference"), Arr( Obj("c", "Contrast", "d", "Delta"), Obj("a", "Apple", "b", "Banana", "t", "Tomato"), Obj("c", "Correlate", "t", "turkey"), Obj("d", "disparity") ) ) );
{ "object": { "t": "turkey", "a": "Apple", "b": "Banana", "c": "Correlate", "d": "disparity" } }
result, err := client.Query( f.Merge( f.Obj{ "c": "Compare", "d": "Difference", }, f.Arr{ f.Obj{ "c": "Contrast", "d": "Delta", }, f.Obj{ "a": "Apple", "b": "Banana", "t": "Tomato" }, f.Obj{ "c": "Correlate", "t": "turkey" }, f.Obj{ "d": "disparity" }}))
map[a:Apple b:Banana c:Correlate d:disparity t:turkey]
System.out.println( client.query( Merge( Obj("c", "Compare", "d", "Difference"), Arr( Obj("c", "Contrast", "d", "Delta"), Obj("a", "Apple", "b", "Banana", "t", "Tomato"), Obj("c", "Correlate", "t", "turkey"), Obj("d", "disparity"), ) ) ).get());
{t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}
client.query( q.Merge( { c: 'Compare', d: 'Difference' }, [ { c: 'Contrast', d: 'Delta' }, { a: 'Apple', b: 'Banana', t: 'Tomato' }, { c: 'Correlate', t: 'turkey' }, { d: 'disparity' }, ], ) ) .then((ret) => console.log(ret))
{ t: 'turkey', a: 'Apple', b: 'Banana', c: 'Compare', d: 'disparity' }
result = client.query( q.merge( { "c": "Compare", "d": "Difference" }, [ { "c": "Contrast", "d": "Delta" }, { "a": "Apple", "b": "Banana", "t": "Tomato" }, { "c": "Correlate", "t": "turkey" }, { "d": "disparity" } ] ) ) print(result)
{'t': 'turkey', 'a': 'Apple', 'b': 'Banana', 'c': 'Correlate', 'd': 'disparity'}
println(Await.result( client.query( Merge( Obj("c" -> "Compare", "d" -> "Difference"), Arr( Obj("c" -> "Contrast", "d" -> "Delta"), Obj("a" -> "Apple", "b" -> "Banana", "t" -> "Tomato"), Obj("c" -> "Correlate", "t" -> "turkey"), Obj("d" -> "disparity"), ) ) ), 5.seconds ))
{t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}
-
The following query demonstrates a merge with a document:
Value result = await client.Query( Merge( Get(Ref(Collection("Letters"), 122)), Obj("x", 10) ) );
{ "object": { "ref": { "@ref": { "id": "122", "collection": { "@ref": { "id": "Letters", "collection": { "@ref": { "id": "collections" } } } } } }, "ts": 1566580631370000, "data": { "object": { "letter": "V", "extra": "22nd" } }, "x": 10 } }
result, err := client.Query( f.Merge( f.Get(f.RefCollection(f.Collection("Letters"), 122)), f.Obj{ "x": 10 }))
map[data:map[extra:22nd letter:V] ref:{122 0xc000092b40 0xc000092b40 <nil>} ts:1566580631370000 x:10]
System.out.println( client.query( Merge( Get(Ref(Collection("Letters"), "122")), Obj("x", Value(10)) ) ).get() );
{ref: ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ts: 1566580631370000, data: {letter: "V", extra: "22nd"}, x: 10}
client.query( q.Merge( q.Get(q.Ref(q.Collection('Letters'), 122)), { x: 10 }, ) ) .then((ret) => console.log(ret))
{ ref: Ref(Collection("Letters"), "122"), ts: 1566580631370000, data: { letter: 'V', extra: '22nd' }, x: 10 }
result = client.query( q.merge( q.get(q.ref(q.collection("Letters"), 122)), { "x": 10 } ) ) print(result)
{'ref': Ref(id=122, collection=Ref(id=Letters, collection=Ref(id=collections))), 'ts': 1566580631370000, 'data': {'letter': 'V', 'extra': '22nd'}, 'x': 10}
println(Await.result(client.query( Merge( Get(Ref(Collection("Letters"), 122)), Obj("x" -> 10), ) ), 5.seconds))
{ref: ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ts: 1566580631370000, data: {letter: "V", extra: "22nd"}, x: 10}
-
The following query demonstrates a merge with the
data
field of a document:Value result = await client.Query( Merge( Select("data", Get(Ref(Collection("Letters"), 122))), Obj("x", 10) ) );
{ "object": { "letter": "V", "extra": "22nd", "x": 10 } }
result, err := client.Query( f.Merge( f.Select("data", f.Get(f.RefCollection(f.Collection("Letters"), 122))), f.Obj{ "x": 10 }))
map[extra:22nd letter:V x:10]
System.out.println( client.query( Merge( Select(Value("data"), Get(Ref(Collection("Letters"), "122"))), Obj("x", Value(10)) ) ).get() );
{letter: "V", extra: "22nd", x: 10}
client.query( q.Merge( q.Select('data', q.Get(q.Ref(q.Collection('Letters'), 122))), { x: 10 }, ) ) .then((ret) => console.log(ret))
{ letter: 'V', extra: '22nd', x: 10 }
print(client.query( q.merge( q.select("data", q.get(q.ref(q.collection("Letters"), 122))), { "x": 10 } ) ))
{'letter': 'V', 'extra': '22nd', 'x': 10}
println(Await.result(client.query( Merge( Select("data", Get(Ref(Collection("Letters"), 122))), Obj("x" -> 10), ) ), 5.seconds))
{letter: "V", extra: "22nd", x: 10}
-
The following query demonstrates a merge situation that provides no benefit:
Value result = await client.Query( Merge( Obj(), Obj("foo", "bar"), Lambda( Arr("key", "a", "b"), Var("a") ) ) );
ObjectV()
result, err := client.Query( f.Merge( f.Obj{}, f.Obj{"foo": "bar"}, f.ConflictResolver( f.Lambda( f.Arr{"key", "a", "b"}, f.Var("a"))))) if (err != nil) { fmt.Println(err) } else { fmt.Println(result) }
map[]
System.out.println( client.query( Merge( Obj(), Obj("foo", Value("bar")), Lambda( Arr(Value("key"), Value("a"), Value("b")), Var("a") ) ) ).get());
{}
client.query( q.Merge( { }, { foo: 'bar' }, q.Lambda( ['key', 'a', 'b'], q.Var('a') ) ) ) .then((ret) => console.log(ret))
{}
result = client.query( q.merge( { }, { "foo": "bar" }, q.lambda_( ["key", "a", "b"], q.var("a") ) ) ) print(result)
{}
println(Await.result( client.query( Merge( Obj(), Obj("foo" -> "bar"), Lambda( Arr("key", "a", "b"), Var("a") ) ) ), 5.seconds ))
{}
The
customResolver
always chooses the value fromobject1
. Since there are no fields inobject1
, the value returned fromcustomResolver
is alwaysnull
. Thenull
return value means that all of the fields inobject2
are removed fromobject1
, which results in an empty object.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!