How can I count the distinct values in a field in MongoDB?

How can I count the distinct values in a field in MongoDB?

MongoDB is a popular NoSQL database management system that allows for efficient and flexible storage of data. One common task in database management is to count the distinct values in a particular field. In MongoDB, this can be achieved using the aggregation framework and the $group operator. The $group operator allows for grouping documents by a specific field and then applying various aggregate functions, such as $sum or $count, to the grouped data. By using the $group operator along with the $addToSet operator, which adds unique values to an array, we can count the number of distinct values in a field. This allows for efficient and accurate counting of unique values, providing valuable insights into the data stored in a MongoDB database.

MongoDB: Count Distinct Values in Field


You can use the following methods to count the distinct values in a specific field in MongoDB:

Method 1: Find List of Distinct Values

db.collection.distinct('field_name')

Method 2: Find Count of Distinct Values

db.collection.distinct('field_name').length

The following examples show how to use each method with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", position: "Guard", points: 31})db.teams.insertOne({team: "Mavs", position: "Guard", points: 22})db.teams.insertOne({team: "Rockets", position: "Center", points: 19})db.teams.insertOne({team: "Rockets", position: "Forward", points: 26})db.teams.insertOne({team: "Cavs", position: "Guard", points: 33})

Example 1: Find List of Distinct Values

We can use the following code to find a list of distinct values in the “team” field:

db.teams.distinct('team')

This query returns the following result:

[ 'Cavs', 'Mavs', 'Rockets' ]

The output shows the three distinct team names from all of the documents.

We could also use the following code to find a list of distinct values in the “position” field:

db.teams.distinct('position')

This query returns the following result:

[ 'Center', 'Forward', 'Guard' ]

Example 2: Find Count of Unique Values

We can use the following code to count the number of distinct values in the “team” field:

db.teams.distinct('team').length

This query returns the following result:

3

This tells us that there are 3 distinct values in the “team” field.

We can also use the following code to count the number of unique values in the “points” field:

db.teams.distinct('points').length

This query returns the following result:

5

This tells us that there are 5 distinct values in the “points” field.

Note: You can find the complete documentation for the distinct function .

Additional Resources

The following tutorials explain how to perform other common operations in MongoDB:

Cite this article

stats writer (2024). How can I count the distinct values in a field in MongoDB?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-distinct-values-in-a-field-in-mongodb/

stats writer. "How can I count the distinct values in a field in MongoDB?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-distinct-values-in-a-field-in-mongodb/.

stats writer. "How can I count the distinct values in a field in MongoDB?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-distinct-values-in-a-field-in-mongodb/.

stats writer (2024) 'How can I count the distinct values in a field in MongoDB?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-distinct-values-in-a-field-in-mongodb/.

[1] stats writer, "How can I count the distinct values in a field in MongoDB?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I count the distinct values in a field in MongoDB?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top