Combining and comparing result sets
Overview
Some types of Fauna queries return one or more groups of results, with the ability to combine and compare those groups in various ways. This page demonstrates several of the Fauna Query Language functions which assist with the combining, comparison, and filtering of query results.
Union
The Union
function performs a logical OR operation on two or more
groups. It returns a de-duplicated group of all the results.
The following example combines the Set Reference returned by
locating the search term fire
in the index named spells_by_element
and the Set Reference returned by locating the search term
water
in the same index. In other words, the query finds all spells
which have either fire
or water
in the element
field. The
Paginate
function materializes the results of the Union
operation into a Page.
ObjectV(data: Arr(RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))), RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections"))), RefV(id = "181388642088911360", collection = RefV(id = "spells", collection = RefV(id = "collections")))))
map[data:[{181388642046968320 0xc000164180 0xc000164180 <nil>} {181388642071085568 0xc000164330 0xc000164330 <nil>} {181388642088911360 0xc0001644e0 0xc0001644e0 <nil>}]]
{data: [ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections")))]}
{
data: [
Ref(Collection("spells"), "181388642046968320"),
Ref(Collection("spells"), "181388642071085568"),
Ref(Collection("spells"), "181388642088911360")
]
}
{'data': [Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))), Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections)))]}
{
data: [
Ref(Collection("spells"), "181388642046968320"),
Ref(Collection("spells"), "181388642071085568"),
Ref(Collection("spells"), "181388642088911360")
]
}
For more information and code examples, see the Union
reference
page.
Intersection
The Intersection
function performs a logical AND operation on two
or more groups. It returns results which are common to all the groups.
The following example intersects the Set Reference returned by
locating the search term fire
in the index named spells_by_element
with the Set Reference returned by locating the search term
water
in the same index. In other words, the query finds all spells
which have both fire
and water
in the element
field.
SetRefV(System.Collections.Generic.Dictionary`2[System.String,FaunaDB.Types.Value])
{map[intersection:[{map[match:{spells_by_element 0xc0001500f0 0xc0001500f0 <nil>} terms:water]} {map[match:{spells_by_element 0xc000150210 0xc000150210 <nil>} terms:fire]}]]}
{@set = {intersection: [{@set = {match: ref(id = "spells_by_element", collection = ref(id = "indexes")), terms: "fire"}}, {@set = {match: ref(id = "spells_by_element", collection = ref(id = "indexes")), terms: "water"}}]}}
Intersection(Match(Index("spells_by_element"), "water"), Match(Index("spells_by_element"), "fire"))
SetRef({'intersection': [SetRef({'match': Ref(id=spells_by_element, collection=Ref(id=indexes)), 'terms': 'water'}), SetRef({'match': Ref(id=spells_by_element, collection=Ref(id=indexes)), 'terms': 'fire'})]})
Intersection(Match(Index("spells_by_element"), "water"), Match(Index("spells_by_element"), "fire"))
For more information and code examples, see the Intersection
reference page.
Difference
The Difference
function compares two groups and returns those
items which are present in the first group but not the second.
The following example takes the Set Reference which is created
by locating the search term fire
in the index named
spells_by_element
and removes all documents in the Set
Reference which is created by locating the search term fire
in the
same index. In other words, the query finds all documents which have
fire
in the element
field, then removes all documents which have
water
in the element
field.
ObjectV(data: Arr(RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections")))))
map[data:[{181388642046968320 0xc000142270 0xc000142270 <nil>}]]
{data: [ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))]}
{ data: [ Ref(Collection("spells"), "181388642046968320") ] }
{'data': [Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections)))]}
{ data: [ Ref(Collection("spells"), "181388642046968320") ] }
For more information and code examples, see the Difference
reference page.
Join
The Join
function joins the items in one group with the items in
the second. The items may be filtered, depending on the type of groups
being joined.
The following example performs a Join
operation on two sets of
documents. The first set is the result of a Match
operation,
which searches the index spellbooks_by_owner
for a particular
character which is specified by ID. The second set is the documents
contained in an index called spells_by_spellbook
. In effect, this
query answers the question "What spells can this character cast?"
ObjectV(data: Arr(ObjectV(ref: RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1626225336060000),data: ObjectV(name: StringV(Fire Beak),element: Arr(StringV(air), StringV(fire)),spellbook: RefV(id = "181388642139243008", collection = RefV(id = "spellbooks", collection = RefV(id = "collections"))))), ObjectV(ref: RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1626225336060000),data: ObjectV(name: StringV(Water Dragon's Claw),element: Arr(StringV(water), StringV(fire)),spellbook: RefV(id = "181388642139243008", collection = RefV(id = "spellbooks", collection = RefV(id = "collections")))))))
map[data:[map[data:map[element:[air fire] name:Fire Beak spellbook:{181388642139243008 0xc000092780 0xc000092780 <nil>}] ref:{181388642046968320 0xc0000925a0 0xc0000925a0 <nil>} ts:1626225336060000] map[data:map[element:[water fire] name:Water Dragon's Claw spellbook:{181388642139243008 0xc000092b40 0xc000092b40 <nil>}] ref:{181388642071085568 0xc000092960 0xc000092960 <nil>} ts:1626225336060000]]]
{data: [{ref: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1626225336060000, data: {name: "Fire Beak", element: ["air", "fire"], spellbook: ref(id = "181388642139243008", collection = ref(id = "spellbooks", collection = ref(id = "collections")))}}, {ref: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1626225336060000, data: {name: "Water Dragon's Claw", element: ["water", "fire"], spellbook: ref(id = "181388642139243008", collection = ref(id = "spellbooks", collection = ref(id = "collections")))}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642046968320"),
ts: 1632781965880000,
data: {
name: 'Fire Beak',
element: [ 'air', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642071085568"),
ts: 1632781965880000,
data: {
name: "Water Dragon's Claw",
element: [ 'water', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
}
]
}
{'data': [{'ref': Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1626225336060000, 'data': {'name': 'Fire Beak', 'element': ['air', 'fire'], 'spellbook': Ref(id=181388642139243008, collection=Ref(id=spellbooks, collection=Ref(id=collections)))}}, {'ref': Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1626225336060000, 'data': {'name': "Water Dragon's Claw", 'element': ['water', 'fire'], 'spellbook': Ref(id=181388642139243008, collection=Ref(id=spellbooks, collection=Ref(id=collections)))}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642046968320"),
ts: 1626225336060000,
data: {
name: 'Fire Beak',
element: [ 'air', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642071085568"),
ts: 1626225336060000,
data: {
name: "Water Dragon's Claw",
element: [ 'water', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
}
]
}
For more information and code examples, see the Join
reference page.
Filter
The Filter
function takes as arguments a set, array, or page of
items and a Lambda
function. The Lambda
function must
return a Boolean, and Filter
returns all items from the group
for which the Lambda
evaluates to true
.
The following example takes the contents of the index
people_by_age_first
and a lambda
function which looks for names
which start with letters between A
and M
. It returns the documents
from the index for which the lambda
function returns true
.
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
data: [
[ 80, 'Leslie' ],
[ 97, 'Alan' ],
[ 107, 'Alan' ],
[ 119, 'Grace' ]
]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
The above example uses Paginate
to materialize the query results
into a Page. It is, however, possible to use Paginate
on the
results of the Match
query to limit the number of documents to
pass on to the lambda
function, as shown in the following example:
The C# version of this example is not currently available.
The Go version of this example is not currently available.
The Java version of this example is not currently available.
{
after: [ 97, 'Alan', Ref(Collection("People"), "326502729981624832") ],
data: [ [ 80, 'Leslie' ] ]
}
The Python version of this example is not currently available.
The Shell version of this example is not currently available.
In the above example, the Paginate
function takes a size
parameter
of 4
, so only the first 4 documents in the index are passed on to the
lambda
function, and we learn that in the first 4 documents of the
index only one name starts with a letter between A
and M
.
For more information and code examples, see the Filter
reference
page.
Range
The Range
function takes three parameters: a set, a start
value, and an end
value. It returns an inclusive subset of the values
from the provided set that includes the range of values starting from
start
up to (and including) end
, as defined by the order of the set.
The following example uses a collection containing the letters of the
alphabet and an index with a values
field defined to contain each
document’s letter
field. See the
indexing tutorials for the query that
creates these documents. The following query returns the range of values
from F to M:
ObjectV(data: Arr(StringV(F), StringV(G), StringV(H), StringV(I), StringV(J), StringV(K), StringV(L), StringV(M)))
map[data:[F G H I J K L M]]
{data: ["F", "G", "H", "I", "J", "K", "L", "M"]}
{ data: [ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' ] }
{'data': ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']}
{ data: [
'F', 'G', 'H',
'I', 'J', 'K',
'L', 'M'
] }
You can also use Range
to return all values up to and including the
end
parameter, or all values including and after the start
value, by
providing an empty array for either the start
or end
value.
The following example returns all letters up to and including 'M':
ObjectV(data: Arr(StringV(A), StringV(B), StringV(C), StringV(D), StringV(E), StringV(F), StringV(G), StringV(H), StringV(I), StringV(J), StringV(K), StringV(L), StringV(M)))
map[data:[A B C D E F G H I J K L M]]
{data: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"]}
{ data:
[ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' ] }
{'data': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']}
{
data: [
'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L',
'M'
]
}
The following example returns all letters after and including 'F':
ObjectV(data: Arr(StringV(F), StringV(G), StringV(H), StringV(I), StringV(J), StringV(K), StringV(L), StringV(M), StringV(N), StringV(O), StringV(P), StringV(Q), StringV(R), StringV(S), StringV(T), StringV(U), StringV(V), StringV(W), StringV(X), StringV(Y), StringV(Z)))
map[data:[F G H I J K L M N O P Q R S T U V W X Y Z]]
{data: ["F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]}
{ data:
[ 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] }
{'data': ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']}
{
data: [
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y',
'Z'
]
}
For more information and code examples, see the Range
reference
page.
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!