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 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(Paginate(Match(Index("elements_of_spells"))));
{
  "data": [ "air", "earth", "fire", "fire", "water", "water" ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "paginate": { "match": { "index": "elements_of_spells" } } }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [ "air", "earth", "fire", "fire", "water", "water" ]
  }
}
result, _ := client.Query(f.Paginate(f.Match(f.Index("elements_of_spells"))))

fmt.Println(result)
map[data:[air earth fire fire water water]]
System.out.println(
    client.query(
      Paginate(Match(Index(Value("elements_of_spells"))))
    ).get());
{
  data: ["air", "earth", "fire", "fire", "water", "water"]
}
client.query(q.Paginate(q.Match(q.Index('elements_of_spells'))))
.then((ret) => console.log(ret))
{ data: [ 'air', 'earth', 'fire', 'fire', 'water', 'water' ] }
client.query(q.paginate(q.match(q.index("elements_of_spells"))))
{
  "data": [ "air", "earth", "fire", "fire", "water", "water" ]
}
client.query(Paginate(Match(Index("elements_of_spells"))))
{
  "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(
  Paginate(Distinct(Match(Index("elements_of_spells")))));
{ "data": [ "air", "earth", "fire", "water" ] }
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "distinct": { "match": { "index": "elements_of_spells" } }
          }
        }'
HTTP/1.1 200 OK
{ "resource": { "data": [ "air", "earth", "fire", "water" ] } }
result, _ := client.Query(
    f.Paginate(
        f.Distinct(f.Match(f.Index("elements_of_spells"))),
    ),
)

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))
{ data: [ 'air', 'earth', 'fire', 'water' ] }
client.query(
  q.paginate(q.distinct(q.match(q.index("elements_of_spells")))))
{ "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));
{
  "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" ]
    }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "distinct": { "match": { "index": "elements_of_spells" } }
          },
          "events": true
        }'
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" ]
      }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Events(
            f.Distinct(f.Match(f.Index("elements_of_spells"))),
        ),
    ),
)

fmt.Println(result)
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]]
]]
System.out.println(
     client.query(
        Events(
          Paginate(Distinct(Match(Index(Value("elements_of_spells")))))
        )
     ).get());
{
  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"]
    }
  ]
}
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] } ] }
client.query(
  q.paginate(
    q.distinct(q.match(q.index("elements_of_spells"))),
    events=True
  ))
{
  "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" ]
    }
  ]
}
client.query(
  Paginate(
    Distinct(Match(Index("elements_of_spells"))),
    events = true))
{
  "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" ]
    }
  ]
}

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"))
  )
);
[
  [
    "A",
    "B",
    "C"
  ],
  [
    "A",
    "B"
  ],
  [
    "A"
  ]
]
Not available in this language yet.
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)
[[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))
[ [ 'A', 'B', 'C' ], [ 'A', 'B' ], [ 'A' ] ]
print(client.query(
  [
    q.distinct(['A', 'B', 'C']),
    q.distinct(['A', 'B', 'A']),
    q.distinct(['A', 'A', 'A']),
  ]
))
[['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"]]

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!