Retrieve Distinct Values of a Field
You can retrieve a list of distinct values for a field across a
collection by calling the distinct()
method on a MongoCollection
object. Pass the document field name as the first parameter and the class
you want to cast the results to as the type parameter.
The following snippets demonstrate the distinct()
method using the movies
collection in the sample_mflix
sample database. Documents are modeled
with the following Kotlin data class:
data class Movie( val type: String, val languages: List<String>, val countries: List<String>, val awards: Awards){ data class Awards(val wins: Int) }
The following method call returns each distinct value of the countries
field in the movies
collection:
collection.distinct<String>(Movie::countries.name)
You can specify a field on the document or one within an embedded document
using dot notation. The following method call returns each distinct
value of the wins
field in the awards
embedded document:
collection.distinct<Int>("${Movie::awards.name}.${Movie.Awards::wins.name}")
You can also limit the set of documents from which your MongoDB instance retrieves distinct values with a query filter as a second parameter, as follows:
collection.distinct<String>(Movie::type.name, Filters.eq(Movie::languages.name, "French"))
The distinct()
method returns an object that implements the
DistinctFlow
class, which contains methods to access, organize, and traverse
the results. DistinctFlow
delegates to the Flow
interface
from the Kotlin Coroutines library, allowing access to methods such as first()
and
firstOrNull()
.
For more information, see our guide on Accessing Data From a Flow.
Example
The following example retrieves a list of distinct values for the year
document field from the movies
collection. It uses a query filter to
match movies that include "Carl Franklin" as one of the values in the
directors
array.
When you run the example, you should see output that reports each distinct year for all the movies that Carl Franklin was included as a director.
Note
This example connects to an instance of MongoDB using a connection URI. To learn more about connecting to your MongoDB instance, see the connection guide.
import com.mongodb.MongoException import com.mongodb.client.model.Filters import com.mongodb.kotlin.client.coroutine.MongoClient import kotlinx.coroutines.runBlocking data class Movie(val year: Int, val directors: List<String>) fun main() = runBlocking { // Replace the uri string with your MongoDB deployment's connection string val uri = "<connection string uri>" val mongoClient = MongoClient.create(uri) val database = mongoClient.getDatabase("sample_mflix") val collection = database.getCollection<Movie>("movies") try { val resultsFlow = collection.distinct<Int>( Movie::year.name, Filters.eq(Movie::directors.name, "Carl Franklin") ) resultsFlow.collect { println(it) } } catch (e: MongoException) { System.err.println("An error occurred: $e") } mongoClient.close() }
1992 1995 1998 ...
For additional information on the classes and methods mentioned on this page, see the following resources:
distinct() API Documentation
distinctFlow API Documentation
Dot Notation Server Manual Entry