• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:11 mins read
You are currently viewing MongoDB distinct() – Get Unique values from field

The MongoDB distinct() is used to get the list of distinct/unique values from a specified field within a collection. Finding distinct values is frequently required to figure out the number of distinct values for particular fields or keys within a collection. In this write-up, we will look at the distinct method for counting the number of unique values per field or key in MongoDB. The following collection is specifically used for the distinct method.

Advertisements

# Create Collection
db. student.insertMany([
      {  
         _id: 1,
         name: "James",
         age: 25,
         gender: "Male",
         course: ["SQL", "MongoDB"]
 
      },
      {
         _id: 2,
         name: "Elena",
         age: 21,
         gender: "Female",
         course: ["Java", "SQL"]
      },
      {
         _id: 3,
         name: "Emily",
         age: 20,
         gender: "Female",
         course: ["MongoDB", "SQL"]
      },
      {
         _id: 4,
         name: "Alex",
         age: 23,
         gender: "Male",
         course: ["Python", "SQL"]
      }
   ]
)

The detailed tutorial about MongoDB can be found here.

1. Retrieve Distinct Values of Field

As we mentioned the distinct method allows us to retrieve the distinct values for a specific field in a MongoDB collection. Following is the syntax of the method.


# Syntax of distinct()
db.collection.distinct(field, query)
  • collection: The name of the collection you want to query.
  • field: The name of the field for which you want to find distinct values.
  • query (optional): A query document that filters the documents from which distinct values will be retrieved. If omitted, all documents in the collection are considered.

Example of using distinct() method.


# Retrieve Distinct Values of Field
db.student.distinct("name")

Here, the distinct function is applied to the name field which enables the extraction of unique names from the student collection, ensuring that each value appears only once in the resulting list.

The following output yielded the unique values from the given field below.

MongoDB Distinct values

2. Retrieve Count of Distinct Values

However, to get the specific number of the distinct values of the field, we called the length function in conjunction with the distinct function. The example is provided just below.


# Retrieve Distinct Values Number of Field
db.student.distinct("name").length

By executing the MongoDB query of a distinct function, we can obtain the count or length of the distinct names retrieved from the name field in the student collection. The length property is appended after the distinct function which enables the calculation of the count or length of the distinct values.

The count or the length retrieved for the distinct names is displayed in the output. 

MongoDB unique values

3. Retrieve Distinct Array Values of Fields

The distinct values of the fields from the collection of MongoDB can also be counted from the array. Below is a distinct function query for acquiring the unique values from the array field.


# Retrieve Distinct Array Values of Fields
db. student.distinct("course")
db. student.distinct("course").length

Here, the distinct function is applied to the course array field, extracting unique values only. Essentially, the purpose of this command is to obtain a list of distinct courses that students are enrolled in from the student collection. Within the next query, the distinct function is called with the length property to determine the count or length of the distinct courses retrieved from the course array field.

The unique courses from the collection and the number of unique courses are yielded in the outcome.

4. Retrieve Distinct Values of Fields From Condition

We can specify the condition in the distinct function to get the distinct values per field as below.


# Retrieve Distinct Values of Fields From Condition
db.student.distinct("name", {"age": {$gt: 23}})

Here, the distinct function is applied to the name field, extracting unique values only. Additionally, a filter is applied using the age” field with the query condition {"age": {$gt: 23}}, which matches documents where the age is greater than 23.

The following output fetches the unique names below where the filter document is matched.

MongoDB unique values

5. Retrieve Distinct Values using runCommand()

Similarly, we can specify the key instead of the field when using the distinct function but using runCommand of MongoDB. Here, we get the distinct value with the key option.


# etrieve Distinct Values of Fields Using Key
db.runCommand ( { distinct: "student", key: "gender" } )

Within the runCommand, the distinct keyword is specified, indicating that we want to retrieve distinct values. The student parameter indicates that we perform a distinct operation on the student collection. After that, the key parameter is employed to specify the field on which the distinct operation is performed; in this case, gender.

Ultimately, the distinct gender values are fetched below.

6. Retrieve Distinct field values from the collection Using Collation

Moreover, to retrieve distinct values from a MongoDB collection while considering collation, we can use the distinct method along with the collation option. This allows us to perform case-insensitive or locale-specific comparisons when retrieving distinct values.


# Retrieve Distinct field values from the collection Using Collation
db. student.distinct( "course", {}, { collation: { locale: "en", strength: 1 } } )

Here, the  course field is subjected to the distinct function, which only extracts unique values. Then, a collation option is provided within the query, which specifies a specific language and strength for string comparisons. While the locale parameter is set to en while configuring the collation option, English is selected as the sorting and comparison language. The collation option here is configured with the locale parameter set to en and the  strength parameter set to 1, which performs case-insensitive comparisons.

The following output can be seen below.

MongoDB Distinct function

7. Conclusion

In conclusion, the distinct method offers a quick and efficient approach to getting distinct values. There, we combined the distinct method with a few additional steps which helped us to easily count the number of distinct values per field/key. The distinct command is handy when you need to quickly retrieve unique values from a specific field in your MongoDB collection, which can be useful for data analysis and reporting.

More details about this topic can be found here.