Docs Menu
Docs Home
/ / /
Scala
/

Retrieve Distinct Field Values

On this page

  • Overview
  • Sample Data
  • Retrieve Distinct Values
  • Retrieve Values Across a Collection
  • Retrieve Values Across Specified Documents
  • Modify Distinct Behavior
  • API Documentation

In this guide, you can learn how to use the Scala driver to retrieve the distinct values of a specified field across a collection.

Within a collection, different documents might contain different values for a single field. For example, one document in a restaurants collection has a borough value of "Manhattan", and another has a borough value of "Queens". By using the Scala driver, you can retrieve all the unique values that a field contains across multiple documents in a collection.

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your Scala application, create a MongoClient that connects to an Atlas cluster and assign the following values to your database and collection variables:

val database: MongoDatabase = client.getDatabase("sample_restaurants")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

To retrieve the distinct values for a specified field, call the distinct() method and pass the name of the field you want to find distinct values for.

The following example retrieves the distinct values of the borough field in the restaurants collection:

collection.distinct("borough")
.subscribe((value: String) => println(value),
(e: Throwable) => println(s"There was an error: $e"))
Bronx
Brooklyn
Manhattan
Missing
Queens
Staten Island

The operation returns an instance of the DistinctObservable class, which you can iterate through to access each distinct borough field value. Although several documents have the same value in the borough field, each value appears in the results only once.

You can provide a query filter to the distinct() method to find distinct field values within a subset of documents in a collection. A query filter is an expression that specifies search criteria used to match documents in an operation. For more information about creating a query filter, see the Specify a Query guide.

The following example retrieves the distinct values of the borough field for all documents that have a cuisine field value of "Italian":

val filter = equal("cuisine", "Italian")
collection.distinct("borough", filter)
.subscribe((value: String) => println(value),
(e: Throwable) => println(s"There was an error: $e"))
Bronx
Brooklyn
Manhattan
Queens
Staten Island

You can modify the behavior of the distinct() method by chaining methods provided by the DistinctObservable class. The following table describes some of these methods:

Method
Description

collation()

Sets the collation to use for the operation.
Parameter Type: Collation

maxTime()

Sets the maximum amount of time that the operation can run.
Parameter Type: Duration

comment()

Attaches a comment to the operation.
Parameter Type: BsonValue or String

first()

Retrieves only the first distinct field value.

The following example retrieves the distinct values of the name field for all documents that have a borough field value of "Bronx" and a cuisine field value of "Pizza". Then, it chains the comment() method to distinct() to add a comment to the operation:

val filter = and(equal("borough", "Bronx"), equal("cuisine", "Pizza"))
collection.distinct("name", filter)
.comment("Bronx Pizza restaurants")
.subscribe((value: String) => println(value),
(e: Throwable) => println(s"There was an error: $e"))
$1.25 Pizza
18 East Gunhill Pizza
2 Bros
Aenos Pizza
Alitalia Pizza Restaurant
Amici Pizza And Pasta
Angie'S Cafe Pizza
...

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Specify Fields to Return