distinct()

Signature

distinct(): Set

Description

The distinct() method returns the unique elements of a set.

The source set isn’t changed.

Avoid using distinct() 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. 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

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 distrinct() 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: "402849420434997314",
  coll: Order,
  ts: Time("2099-07-08T13:39:55.690Z"),
  // The `customer` field contains a `Customer` collection document.
  customer: {
    id: "402849420422414402",
    coll: Customer,
    ts: Time("2099-07-08T13:39:55.690Z"),
    firstName: "Alice",
    lastName: "Appleseed",
    email: "alice.appleseed@example.com",
    ...
  },
  cart: [
    {
      // The `product` field contains a `Product` collection document.
      product: {
        id: "402849420408782914",
        coll: Product,
        ts: Time("2024-07-08T13:39:55.690Z"),
        name: "avocados",
        description: "Conventional Hass, 4ct bag",
        price: 3.99,
        quantity: 995,
        store: Store("402849420398297154"),
        backorderLimit: 15,
        backordered: false
      }
      quantity: 5,
      price: 3.99
    },
    ...
  ],
  status: "processing",
}

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 `cart` field that contains an array
  // of objects. Each object includes a `product` field. This
  // `flatMap()` call converts the object array to a set,
  // extracts the `id` and `name` of each product, and flattens the
  // resulting set.
  .flatMap(.cart.map(.product).toSet() { id, name } )
  // Deduplicates the set of product `id` and `name` values so that
  // it only returns unique elements.
  .distinct()
{
  data: [
    {
      id: "402849420408782914",
      name: "avocados"
    },
    {
      id: "402849420410880066",
      name: "limes"
    },
    {
      id: "402849420411928642",
      name: "limes"
    },
    {
      id: "402849420417171522",
      name: "cilantro"
    }
  ]
}

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!