Distinct
Distinct( source )
Description
The Distinct
function returns all of the unique items found in
source
, which can be an Array or
Set.
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 array or set to evaluate for distinct elements. |
Returns
When source
is an array, an array of the distinct items found in
source
.
When source
is a set reference, a set reference of the distinct items
found in source
.
Examples
The following query shows all of the elements in the "elements_of_spells" index. The index contains duplicate values for "fire" and "water".
client.query(q.Paginate(q.Match(q.Index('elements_of_spells'))))
.then((ret) => console.log(ret))
{ data: [ 'air', 'earth', 'fire', 'fire', 'water', 'water' ] }
When the set operator Distinct
is applied to this query, the duplicate
values, "fire" and "water" are eliminated.
client.query(q.Paginate(q.Distinct(q.Match(q.Index('elements_of_spells')))))
.then((ret) => console.log(ret))
{ data: [ 'air', 'earth', 'fire', 'water' ] }
The events view of a set of values include the resources themselves, the
distinct
function returns the same set.
client.query(
q.Paginate(
q.Events(q.Distinct(q.Match(q.Index('elements_of_spells'))))
)
)
.then((ret) => console.log(ret))
{ data:
[ { ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'add',
document:
Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'remove',
document:
Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] },
{ ts: 1527095186458101,
action: 'remove',
document:
Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))),
data: [Array] } ] }
The following query demonstrates how various arrays are evaluated:
client.query([
q.Distinct(['A', 'B', 'C']),
q.Distinct(['A', 'B', 'A']),
q.Distinct(['A', 'A', 'A']),
])
.then((ret) => console.log(ret))
[ [ 'A', 'B', 'C' ], [ 'A', 'B' ], [ 'A' ] ]
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!