Overview
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.
Sample Data
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 theRestaurantstruct, defined in the code examples
Retrieve Distinct Values
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.
Retrieve Values Across a Collection
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.
Retrieve Values Across Specified Documents
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")
Modify Distinct Behavior
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 |
|---|---|
| The collation to use for the operation. |
| The index to use for the operation. |
| The comment to attach to the operation. |
| The maximum amount of time that the operation can run. |
| The read concern to use for the operation. |
| The read preference and tags to use for the operation. |
API Documentation
To learn more about the distinct() method, see the API documentation.