MongoDB

How to use distinct count query in MongoDB

MongoDB being a NoSQL database does not follow the retrieval mechanism of traditional databases. It has its own MongoDB Query Language(MQL) that contains an extensive list of methods and commands to store and retrieve documents in MongoDB. One of the retrieval queries is to display the distinct number of documents in a specific collection.

The distinct count query is helpful when you have hundreds of documents in a collection, and you want to find the total number of distinct documents in that collection. In today’s post of the MongoDB tutorial series, we have described the basic concept of distinct count query and its application in MongoDB.

How does distinct count works

The primary purpose of counting distinct documents is to avoid the duplication that can consume time and resources while querying. The syntax of the distinct method is provided below:

db.collection-name.distinct("<field>", "<query>", "<options>").length

By using the above-mentioned syntax, the distinct fields are retrieved by using the distinct() method whereas the “.length” will count the number of fields returned by the distinct() method.

Prerequisites

There are a few MongoDB-based Ubuntu instances that must be ready to get to the practice session. For instance, the following things you must ensure:

Database: A valid MongoDB database is required to be on your Ubuntu. For instance, we are using a database that is named “linuxhint“.

Collection: After the database, a collection is necessary and must be associated with your database. The collection name used in this guide is “laptops“.

The upcoming section demonstrates the usage of the distinct count method in MongoDB.

How to use distinct count method in MongoDB

Before exploring the working with some examples, let’s have a look at the content inside our “laptops” collection:

> db.laptops.find().pretty()

Text

Description automatically generated Text

Description automatically generated

The examples in this guide will be practiced on the data shown above.

Example 1: Getting the distinct field names in the “Cat” field

In this example, the distinct() method is applied on the “Cat” field and it will return the names of distinct fields in the “laptops” collection. For this we have executed the following command in MongoDB Shell.

> db.laptops.distinct("Cat")

Text

Description automatically generated

As it is observed that the “distinct()” method only displays the names of distinct fields.

Example 2: Counting the number of distinct values in “Cat” field

Referring to the example above, we will use the below mentioned command to count the number of distinct fields in “Cat” fields of “linuxhint” collection.

> db.laptops.distinct("Cat").length

A picture containing text

Description automatically generated

Example 3: Using a query condition

In this example, the distinct method will be used with the query condition and in such a situation, only those values are returned that are distinct as well as matches the query condition. For instance, the below mentioned command will give you the count of distinct values in “Make” field where the condition must match [ Cat: “Gaming’ ]:

> db.laptops.distinct("Make", {Cat: "Gaming"}).length

A picture containing text

Description automatically generated

It can be seen from the output that there are “4” distinct fields that have a “Make” field and in those “Cat” matches “Gaming“.

Example 4: Counting the number of distinct values in array field

The “Make” field in the “laptops” collection is an array that contains the manufacturer’s name in it. For instance, the below mentioned command will count number of distinct values in it:

> db.laptops.distinct("Make").length

A picture containing text

Description automatically generated

Example 5: Counting the number of distinct values in a numeric field

The distinct method can be applied on numeric data types in MongoDB as well. As in “laptops” collection; there is a field “Price” and the values stored belong to “double” datatype. The command written below will count the number of distinct values in the “Price” field.

> db.laptops.distinct("Price").length

Diagram, website

Description automatically generated with medium confidence

Conclusion

MongoDB retrieves documents like any other database does, and it also has a distinct() function to retrieve only distinct values of any field. In this article of the MongoDB series, you have learned to count distinct field values and their length as well. The documents retrieved are counted with the help of the .length extension of the distinct method of MongoDB. Additionally, the distinct count can be applied to any kind of data type that is supported by MongoDB.

About the author

Adnan Shabbir