Distinct

Distinct( source )
Distinct( source )
Distinct( source )
Distinct( source )
distinct( source )
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 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 setref, a setref 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(
    Paginate(Match(Index("elements_of_spells")))
);
ObjectV(data: Arr(StringV(air), StringV(air), StringV(earth), StringV(fire), StringV(fire), StringV(water), StringV(water)))
result, err := client.Query(
	f.Paginate(f.Match(f.Index("elements_of_spells"))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[air air earth fire fire water water]]
System.out.println(
    client.query(
        Paginate(Match(Index(Value("elements_of_spells"))))
    ).get());
{data: ["air", "air", "earth", "fire", "fire", "water", "water"]}
client.query(
  q.Paginate(q.Match(q.Index('elements_of_spells')))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  data: [
    'air',   'air',
    'earth', 'fire',
    'fire',  'water',
    'water'
  ]
}
result = client.query(
  q.paginate(q.match(q.index("elements_of_spells")))
)
print(result)
{'data': ['air', 'air', 'earth', 'fire', 'fire', 'water', 'water']}
client.query(Paginate(Match(Index("elements_of_spells"))))
{data: ["air", "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(
    Paginate(Distinct(Match(Index("elements_of_spells"))))
);
ObjectV(data: Arr(StringV(air), StringV(earth), StringV(fire), StringV(water)))
result, err := client.Query(
	f.Paginate(
		f.Distinct(f.Match(f.Index("elements_of_spells")))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[air earth fire water]]
System.out.println(
    client.query(
        Paginate(Distinct(Match(Index(Value("elements_of_spells")))))
    ).get());
{data: ["air", "earth", "fire", "water"]}
client.query(
  q.Paginate(q.Distinct(q.Match(q.Index('elements_of_spells'))))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{ data: [ 'air', 'earth', 'fire', 'water' ] }
result = client.query(
  q.paginate(q.distinct(q.match(q.index("elements_of_spells"))))
)
print(result)
{'data': ['air', 'earth', 'fire', 'water']}
client.query(
  Paginate(Distinct(Match(Index("elements_of_spells")))))
{ "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(
    Paginate(
        Distinct(Match(Index("elements_of_spells"))),
        events: true
    )
);
ObjectV(data: Arr(ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(air))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(fire))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(fire))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(water))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642088911360", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(earth))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642088911360", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(water))), ObjectV(ts: LongV(1603756252570000),action: StringV(add),document: RefV(id = "181388642581742080", collection = RefV(id = "spells", collection = RefV(id = "collections"))),data: Arr(StringV(air)))))
result, err := client.Query(
	f.Paginate(
		f.Events(
			f.Distinct(f.Match(f.Index("elements_of_spells"))))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[map[action:add data:[air] document:{181388642046968320 0xc00009a210 0xc00009a210 <nil>} ts:1603747155400000] map[action:add data:[fire] document:{181388642046968320 0xc00009a3f0 0xc00009a3f0 <nil>} ts:1603747155400000] map[action:add data:[fire] document:{181388642071085568 0xc00008e210 0xc00008e210 <nil>} ts:1603747155400000] map[action:add data:[water] document:{181388642071085568 0xc00008e3f0 0xc00008e3f0 <nil>} ts:1603747155400000] map[action:add data:[earth] document:{181388642088911360 0xc00008e5d0 0xc00008e5d0 <nil>} ts:1603747155400000] map[action:add data:[water] document:{181388642088911360 0xc00008e7b0 0xc00008e7b0 <nil>} ts:1603747155400000] map[action:add data:[air] document:{181388642581742080 0xc00008e990 0xc00008e990 <nil>} ts:1603747155400000]]]
System.out.println(
    client.query(
        Paginate(
            Events(
                Distinct(Match(Index(Value("elements_of_spells"))))
            )
        )
    ).get());
{data: [{ts: 1594321463870000, action: "add", document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["air"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["fire"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["fire"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["water"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["earth"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["water"]}, {ts: 1594321463870000, action: "add", document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["air"]}]}
client.query(
  q.Paginate(
    q.Events(q.Distinct(q.Match(q.Index('elements_of_spells'))))
  )
)
.then((ret) => console.log(util.inspect(ret, { depth: null })))
.catch((err) => console.error('Error: %s', err))
{
  data: [
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642046968320"),
      data: [ 'air' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642046968320"),
      data: [ 'fire' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642071085568"),
      data: [ 'fire' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642071085568"),
      data: [ 'water' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642088911360"),
      data: [ 'earth' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642088911360"),
      data: [ 'water' ]
    },
    {
      ts: 1592270149090000,
      action: 'add',
      document: Ref(Collection("spells"), "181388642581742080"),
      data: [ 'air' ]
    }
  ]
}
result = client.query(
  q.paginate(
    q.distinct(q.match(q.index("elements_of_spells"))),
    events=True
  )
)
print(result)
{'data': [{'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['air']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['fire']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['fire']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['water']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['earth']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['water']}, {'ts': 1592864274420000, 'action': 'add', 'document': Ref(id=181388642581742080, collection=Ref(id=spells, collection=Ref(id=collections))), 'data': ['air']}]}
client.query(
  Paginate(
    Distinct(Match(Index("elements_of_spells"))),
    events = true))
{data: [{ts: 1594486915560000, action: "add", document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["air"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["fire"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["fire"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["water"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["earth"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["water"]}, {ts: 1594486915560000, action: "add", document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))), data: ["air"]}]}

The following query demonstrates how various arrays are evaluated:

Value result = await client.Query(
    Arr(
        Distinct(Arr("A", "B", "C")),
        Distinct(Arr("A", "B", "A")),
        Distinct(Arr("A", "A", "A"))
    )
);
Arr(Arr(StringV(A), StringV(B), StringV(C)), Arr(StringV(A), StringV(B)), Arr(StringV(A)))
result, err := client.Query(
	f.Arr{
		f.Distinct(f.Arr{"A", "B", "C"}),
		f.Distinct(f.Arr{"A", "B", "A"}),
		f.Distinct(f.Arr{"A", "A", "A"}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[[A B C] [A B] [A]]
System.out.println(
    client.query(
        Arr(
            Distinct(
                Arr(Value("A"), Value("B"), Value("C"))
            ),
            Distinct(
                Arr(Value("A"), Value("B"), Value("A"))
            ),
            Distinct(
                Arr(Value("A"), Value("A"), Value("A"))
            )
        )
    ).get());
[["A", "B", "C"], ["A", "B"], ["A"]]
client.query([
  q.Distinct(['A', 'B', 'C']),
  q.Distinct(['A', 'B', 'A']),
  q.Distinct(['A', 'A', 'A']),
])
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
[ [ 'A', 'B', 'C' ], [ 'A', 'B' ], [ 'A' ] ]
result = client.query(
  [
    q.distinct(['A', 'B', 'C']),
    q.distinct(['A', 'B', 'A']),
    q.distinct(['A', 'A', 'A']),
  ]
)
print(result)
[['A', 'B', 'C'], ['A', 'B'], ['A']]
println(Await.result(
  client.query(
    Arr(
      Distinct(Arr("A", "B", "C")),
      Distinct(Arr("A", "B", "A")),
      Distinct(Arr("A", "A", "A"))
    )
  ),
  5.seconds
))
[["A", "B", "C"], ["A", "B"], ["A"]]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!