Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

set.distinct()

Get the unique elements of a Set.

Signature

distinct() => Set<A>

Description

Gets the unique elements of the calling Set.

The calling Set isn’t changed.

Eager loading

This method uses eager loading and requires a read of each document in the calling Set. For large Sets, this may result in poor performance and high costs.

Performance hint: full_set_read

Queries that call this method on a document Set emit a performance hint, if enabled. For example, the following query:

// Use `distinct()` to get all unique products
Product.all().distinct()

Emits the following hint:

performance_hint: full_set_read - Using distinct() causes the full set to be read. See https://docs.fauna.com/performance_hint/full_set_read.
at *query*:2:23
  |
2 | Product.all().distinct()
  |                       ^^
  |

To address the hint, use set.take() to explicitly limit the size of the calling Set to fewer than 100 documents:

// Limit the doc Set's size using `take()`
Product.all().take(20).distinct()

This applies even if the original, unbounded Set contains fewer than 100 documents.

Alternatively, you can rewrite the query to avoid calling the method.

Avoid use on large Sets

Avoid using distinct() on large or unbounded Sets that contain 16,000 or more documents.

If a Set contains 16,000 or more documents, the query requires pagination. array.distinct() would only be able to extract unique elements from each page of results.

Instead, retrieve all field values and process them on the client side. See Get unique field values.

Parameters

None

Return value

Type Description

Set<Generic>

Unique elements in the Set.

Examples

Basic example

// `toSet()` converts an Array to a Set.
let set = [1, 1, 2, 3, 3].toSet()
set.distinct()
{
  data: [
    1,
    2,
    3
  ]
}

Get unique products ordered by a customer

In this example, you’ll use distinct() to get a Set of unique products ordered by a specific customer.

The example uses the Order collection. Order collection documents have the following structure:

{
  id: "12345",
  coll: Order,
  ts: Time("2099-07-31T12:42:19Z"),
  // `items` contains a Set of `OrderItem` documents.
  items: {
    data: [
      {
        id: "112233",
        coll: OrderItem,
        ts: Time("2099-07-31T12:42:19Z"),
        order: Order("12345"),
        product: Product("111"),
        quantity: 2
      },
      ...
    ]
  },
  total: 5392,
  status: "cart",
  // `customer` contains a `Customer` document.
  customer: {
    id: "111",
    coll: Customer,
    ts: Time("2099-07-31T12:42:19Z"),
    cart: Order("412483941752112205"),
    // `orders` contains a Set of `OrderItem` documents.
    orders: "hdW...",
    name: "Alice Appleseed",
    email: "alice.appleseed@example.com",
    address: {
      street: "87856 Mendota Court",
      city: "Washington",
      state: "DC",
      postalCode: "20220",
      country: "US"
    }
  },
  createdAt: Time("2099-07-31T12:42:18.774426Z"),
  payment: {}
}

The query:

// Uses the `Customer` collection's `byEmail()` index to
// get `Customer` collection documents by `email` field value.
// In the `Customer` collection, `email` field values are unique
// so return the `first()` (and only) document.
let customer = Customer.byEmail("alice.appleseed@example.com").first()

// Uses the `Order` collection's `byCustomer()` index to
// get `Order` collection documents for the previous customer.
Order.byCustomer(customer)
  // `Order` documents include a `items` field that contains a Set
  // of `OrderItem` documents. Each `OrderItem` document includes a
  // `product` field. This `flatMap()` call extracts the `id` and
  // `name` of each product, and flattens the resulting Set.
  .flatMap(.items.map(.product) { id, name } )
  // Deduplicates the Set of product `id` and `name` values so that
  // it only returns unique elements.
  .distinct()
{
  data: [
    {
      id: "111",
      name: "cups"
    },
    {
      id: "222",
      name: "donkey pinata"
    },
    {
      id: "333",
      name: "pizza"
    }
  ]
}

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!