Difference
Difference( source, diff, ... )
Description
The Difference
function compares the source
, which can be an
Array or Set,
with the item(s) provided by diff
, and returns all of the items that
exist in source
that do not exist in diff
.
The run time of To work around this, you may specify a larger query timeout via the driver that you are using. |
Parameters
Argument | Type | Definition and Requirements |
---|---|---|
|
The source array or set to be compared.
The type of |
|
|
One or more difference array or set reference objects.
The type of all items in |
Returns
When source
is an array, an array of the items in source
that are
missing from diff
.
When source
is a set reference, a set reference object of the items in
source
that are missing from diff
.
Examples
The following query takes the source Set Reference (i.e. set reference)
which is created by locating the search term "fire" in the index named
"spells_by_element" and removing all difference Set Reference which was
created by locating the search term "water" in the Index named
"spells_by_element". The Paginate
function materialized the results of the Difference
operation in an
array of type Page.
client.query(
q.Paginate(
q.Difference(
q.Match(q.Index('spells_by_element'), 'fire'),
q.Match(q.Index('spells_by_element'), 'water'),
)
)
)
.then((ret) => console.log(ret))
{ data:
[ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
The following query is similar to the example above, but it returns document events instead of the index tuples.
client.query(
q.Paginate(
q.Events(
q.Difference(
q.Match(q.Index('spells_by_element'), 'fire'),
q.Match(q.Index('spells_by_element'), 'water'),
)
)
)
)
.then((ret) => console.log(ret))
{ data:
[ { ts: 1526677776479051,
action: 'add',
document:
Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) },
{ ts: 1526677776479051,
action: 'add',
document:
Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) } ] }
The following query demonstrates how various arrays are compared:
client.query([
q.Difference(['A', 'B', 'C'], ['B', 'C', 'D']),
q.Difference(['B', 'C', 'D'], ['A', 'B', 'C']),
q.Difference(['A', 'B', 'C'], ['C', 'B', 'A']),
])
.then((ret) => console.log(ret))
[ [ 'A' ], [ 'D' ], [] ]
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!