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.

For query timeout errors, you may specify a larger query timeout via the driver that you are using.

Parameters

Parameter Type Definition and Requirements

source

The Array or Set Reference 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))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    'air',   'air',
    'earth', 'fire',
    'fire',  'water',
    'water'
  ]
}
Query metrics:
  •    bytesIn:  64

  •   bytesOut:  73

  • computeOps:   1

  •    readOps:   8

  •   writeOps:   0

  •  readBytes: 466

  • writeBytes:   0

  •  queryTime: 7ms

  •    retries:   0

When the Distinct function 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))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{ data: [ 'air', 'earth', 'fire', 'water' ] }
Query metrics:
  •    bytesIn:   77

  •   bytesOut:   52

  • computeOps:    1

  •    readOps:    8

  •   writeOps:    0

  •  readBytes:  466

  • writeBytes:    0

  •  queryTime: 11ms

  •    retries:    0

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(util.inspect(ret, { depth: null })))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642046968320"),
      data: [ 'air' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642046968320"),
      data: [ 'fire' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642071085568"),
      data: [ 'fire' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642071085568"),
      data: [ 'water' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642088911360"),
      data: [ 'earth' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642088911360"),
      data: [ 'water' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: (("spells"), "181388642581742080"),
      data: [ 'air' ]
    }
  ]
}
Query metrics:
  •    bytesIn:    88

  •   bytesOut: 1,312

  • computeOps:     1

  •    readOps:     8

  •   writeOps:     0

  •  readBytes:   466

  • writeBytes:     0

  •  queryTime:  10ms

  •    retries:     0

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))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ [ 'A', 'B', 'C' ], [ 'A', 'B' ], [ 'A' ] ]
Query metrics:
  •    bytesIn:  82

  •   bytesOut:  44

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

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!