Distinct

Copied!
( 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 Distinct is dependent on the size of the underlying set or page, and the exclusivity of the result. For large sets or pages with many non-exclusive items, executing Distinct might result in a query timeout error.

To work around this, you may specify a larger query timeout via the driver that you are using.

Parameters

Argument Type Definition and Requirements

source

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".

Copied!
client.query(q.(q.(q.('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.

Copied!
client.query(q.(q.(q.(q.('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.

Copied!
client.query(
  q.(
    q.(q.(q.(q.('elements_of_spells'))))
  )
)
.then((ret) => console.log(ret))
{ data:
  [  { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642046968320, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642046968320, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642071085568, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642071085568, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642088911360, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642088911360, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642581742080, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'add',
       document:
        (id=181388642581742080, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'remove',
       document:
        (id=181388642581742080, collection=(id=spells, collection=(id=collections))),
       data: [Array] },
     { ts: 1527095186458101,
       action: 'remove',
       document:
        (id=181388642581742080, collection=(id=spells, collection=(id=collections))),
       data: [Array] } ] }

The following query demonstrates how various arrays are evaluated:

Copied!
client.query([
  q.(['A', 'B', 'C']),
  q.(['A', 'B', 'A']),
  q.(['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!