Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Retrieve Distinct Field Values

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

Within a collection, 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". You can use the Rust driver to retrieve the values "Manhattan", "Queens", and all other unique values in the borough field across all documents in the restaurants collection.

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.

You can access the documents in the restaurants collection as instances of the Document type or a custom data type. To specify which data type represents the collection's data, replace the <T> type parameter with one of the following values:

  • <Document>: Represents collection documents as BSON documents

  • <Restaurant>: Represents collection documents as instances of the Restaurant struct, defined in the code examples

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

The distinct() method returns the list of distinct values as a Vec<Bson> object, a vector of Bson values.

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

Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:

let boroughs = my_coll.distinct("borough", None).await?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Bronx")
String("Brooklyn")
String("Manhattan")
String("Missing")
String("Queens")
String("Staten Island")
let boroughs = my_coll.distinct("borough", None).run()?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Bronx")
String("Brooklyn")
String("Manhattan")
String("Missing")
String("Queens")
String("Staten Island")

The operation returns a vector that stores 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 the distinct field values across 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 example, you can use the distinct() method to find distinct field values from only a subset of matched documents.

Tip

To learn more 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 "Turkish".

Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:

let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter).await?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")
let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter).run()?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")

You can modify the behavior of the distinct() method by chaining one or more option methods to your count_documents() call before the await or run() method call. The following table describes options you can set to customize the distinct operation:

Option
Description

collation()

The collation to use for the operation.
Type: Collation

hint()

The index to use for the operation.
Type: Hint

comment()

The comment to attach to the operation.
Type: Bson

max_time()

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

read_concern()

The read concern to use for the operation.
Type: ReadConcern

selection_criteria()

The read preference and tags to use for the operation.
Type: SelectionCriteria

To learn more about the distinct() method, see the API documentation.

Back

Count Documents

On this page