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.

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

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "paginate": { "match": { "index": "elements_of_spells" } } }'
client.Query(Paginate(Match(Index("elements_of_spells"))));
System.out.println(
    client.query(
      Paginate(Match(Index(Value("elements_of_spells"))))
    ).get());
result, _ := client.Query(f.Paginate(f.Match(f.Index("elements_of_spells"))))

fmt.Println(result)
client.query(Paginate(Match(Index("elements_of_spells"))))
client.query(q.paginate(q.match(q.index("elements_of_spells"))))
client.query(q.Paginate(q.Match(q.Index('elements_of_spells'))))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "data": [ "air", "earth", "fire", "fire", "water", "water" ]
  }
}
{
  "data": [ "air", "earth", "fire", "fire", "water", "water" ]
}
{
  data: ["air", "earth", "fire", "fire", "water", "water"]
}
map[data:[air earth fire fire water water]]
{
  "data": [ "air", "earth", "fire", "fire", "water", "water" ]
}
{
  "data": [ "air", "earth", "fire", "fire", "water", "water" ]
}
{ 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.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "distinct": { "match": { "index": "elements_of_spells" } }
          }
        }'
client.Query(
  Paginate(Distinct(Match(Index("elements_of_spells")))));
System.out.println(
        client.query(
           Paginate(Distinct(Match(Index(Value("elements_of_spells")))))
        ).get());
result, _ := client.Query(
    f.Paginate(
        f.Distinct(f.Match(f.Index("elements_of_spells"))),
    ),
)

fmt.Println(result)
client.query(
  Paginate(Distinct(Match(Index("elements_of_spells")))))
client.query(
  q.paginate(q.distinct(q.match(q.index("elements_of_spells")))))
client.query(q.Paginate(q.Distinct(q.Match(q.Index('elements_of_spells')))))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{ "resource": { "data": [ "air", "earth", "fire", "water" ] } }
{ "data": [ "air", "earth", "fire", "water" ] }
{ data: ["air", "earth", "fire", "water"] }
map[data:[air earth fire water]]
{ "data": [ "air", "earth", "fire", "water" ] }
{ "data": [ "air", "earth", "fire", "water" ] }
{ data: [ 'air', 'earth', 'fire', 'water' ] }

The events view of a set of values include the resources themselves, the distinct function returns the same set.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "distinct": { "match": { "index": "elements_of_spells" } }
          },
          "events": true
        }'
client.Query(
  Paginate(
    Distinct(Match(Index("elements_of_spells"))),
    events: true));
System.out.println(
     client.query(
        Events(
          Paginate(Distinct(Match(Index(Value("elements_of_spells")))))
        )
     ).get());
result, _ := client.Query(
    f.Paginate(
        f.Events(
            f.Distinct(f.Match(f.Index("elements_of_spells"))),
        ),
    ),
)

fmt.Println(result)
client.query(
  Paginate(
    Distinct(Match(Index("elements_of_spells"))),
    events = true))
client.query(
  q.paginate(
    q.distinct(q.match(q.index("elements_of_spells"))),
    events=True
  ))
client.query(
  q.Paginate(
    q.Events(q.Distinct(q.Match(q.Index('elements_of_spells'))))
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1509244539203043,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642046968320" },
        "values": [ "air" ]
      },
      {
        "ts": 1509244539203043,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642046968320" },
        "values": [ "fire" ]
      },
      {
        "ts": 1509244539223511,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642071085568" },
        "values": [ "fire" ]
      },
      {
        "ts": 1509244539223511,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642071085568" },
        "values": [ "water" ]
      },
      {
        "ts": 1509244539235128,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642088911360" },
        "values": [ "earth" ]
      },
      {
        "ts": 1509244539235128,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642088911360" },
        "values": [ "water" ]
      },
      {
        "ts": 1509244539709690,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642581742080" },
        "values": [ "air" ]
      },
      {
        "ts": 1509244539764856,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642581742080" },
        "values": [ "earth" ]
      },
      {
        "ts": 1509244539795464,
        "action": "delete",
        "resource": { "@ref": "classes/spells/181388642581742080" },
        "values": [ "air" ]
      },
      {
        "ts": 1509244539795464,
        "action": "delete",
        "resource": { "@ref": "classes/spells/181388642581742080" },
        "values": [ "earth" ]
      }
    ]
  }
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539709690,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539764856,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    }
  ]
}
{
  data: [
    {
      ts: 1527267261636139,
      action: "add",
      document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["fire"]
    {
      ts: 1527267261636139,
      action: "add",
      document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["water"]
    {
      ts: 1527267261664785,
      action: "add",
      document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["air"]
    {
      ts: 1527267261664785,
      action: "add",
      document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["fire"]
    {
      ts: 1527267261713947,
      action: "add",
      document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["earth"]
    {
      ts: 1527267261713947,
      action: "add",
      document: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["water"]
    {
      ts: 1527267261924953,
      action: "add",
      document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["air"]
    {
      ts: 1527267261964739,
      action: "remove",
      document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["air"]
    {
      ts: 1527267262013935,
      action: "add",
      document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["air"]
    {
      ts: 1527267262062984,
      action: "remove",
      document: ref(id = "181388642581742080", collection = ref(id = "spells", collection = ref(id = "collections"))),
      data: ["air"]
    }
  ]
}
map[data:[
  map[ts:1509244539203043 action:add document:{181388642046968320 0xc42026c920 <nil>} data:[air]]
  map[ts:1509244539203043 action:add document:{181388642046968320 0xc42026cc40 <nil>} data:[fire]]
  map[ts:1509244539223511 action:add document:{181388642071085568 0xc42026cf80 <nil>} data:[fire]]
  map[ts:1509244539223511 action:add document:{181388642071085568 0xc42026d2a0 <nil>} data:[water]]
  map[ts:1509244539235128 action:add document:{181388642088911360 0xc42026d5c0 <nil>} data:[earth]]
  map[ts:1509244539235128 action:add document:{181388642088911360 0xc42026d8e0 <nil>} data:[water]]
  map[ts:1509244539709690 action:add document:{181388642581742080 0xc42026dc00 <nil>} data:[air]]
  map[ts:1509244539764856 action:add document:{181388642581742080 0xc42026df20 <nil>} data:[earth]]
  map[ts:1509244539795464 action:remove document:{181388642581742080 0xc420284240 <nil>} data:[air]]
  map[ts:1509244539795464 action:remove document:{181388642581742080 0xc420284560 <nil>} data:[earth]]
]]
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539709690,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539764856,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    }
  ]
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "fire" ]
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539235128,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642088911360" },
      "values": [ "water" ]
    },
    {
      "ts": 1509244539709690,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539764856,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "air" ]
    },
    {
      "ts": 1509244539795464,
      "action": "delete",
      "resource": { "@ref": "classes/spells/181388642581742080" },
      "values": [ "earth" ]
    }
  ]
}
{ 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:

Value result = await client.Query(
  Arr(
    Distinct(Arr("A", "B", "C")),
    Distinct(Arr("A", "B", "A")),
    Distinct(Arr("A", "A", "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"})})
fmt.Println(result)
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());
client.query([
  q.Distinct(['A', 'B', 'C']),
  q.Distinct(['A', 'B', 'A']),
  q.Distinct(['A', 'A', 'A']),
])
.then((ret) => console.log(ret))
print(client.query(
  [
    q.distinct(['A', 'B', 'C']),
    q.distinct(['A', 'B', 'A']),
    q.distinct(['A', 'A', 'A']),
  ]
))
println(Await.result(
  client.query(
    Arr(
      Distinct(Arr("A", "B", "C")),
      Distinct(Arr("A", "B", "A")),
      Distinct(Arr("A", "A", "A"))
    )
  ),
  5.seconds
))
Not available in this language yet.
[
  [
    "A",
    "B",
    "C"
  ],
  [
    "A",
    "B"
  ],
  [
    "A"
  ]
]
[[A B C] [A B] [A]]
[["A", "B", "C"], ["A", "B"], ["A"]]
[ [ 'A', 'B', 'C' ], [ 'A', 'B' ], [ 'A' ] ]
[['A', 'B', 'C'], ['A', 'B'], ['A']]
[["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!