In today’s MongoDB tutorial series post, we shall discuss the fundamental notion of distinct count queries and their use in MongoDB. Show
Utilizing the distinct count query in MongoDBThe main reason for counting different documents is to eliminate duplication, which can waste time and resources when querying. The syntax of the distinct method is as follows: db.collection-name.distinct("<field>", "<query>", "<options>").length From the above command, the separate fields are retrieved by using the distinct() function, and the “.length” variable counts the number of fields provided by the distinct() method. PrerequisitesA few MongoDB-based Ubuntu instances must be available to get to the practice session. For example, you must verify you have the following prerequisites:
The following section depicts how to use the distinct count function in MongoDB. The distinct count method in useBefore we start with some samples, let’s look at the items in our “fosslinuxtuts” collection. To do so, execute the following line of code: db.fosslinuxtuts.find().pretty() Check items in our collection We shall use the content in our collection to try out some examples that will aid us in understanding how to use the distinct count query in MongoDB. Note: If you have not created any inputs on your collection, execute this line of code to create a new collection: db.fosslinuxtuts.insertMany([ {Name: "Abraham", Designation: ["Author", "Junior"], WriterCode: 01}, {Name: "Emmanuel", Designation: ["Author", "Junior"], WriterCode: 02}, {Name: "Hend", Designation: ["Author", "Junior"], WriterCode: 03} ]) Insert documents in a collection Once you have items in your collection, you can proceed and try out the examples provided herein: Example 1: Retrieving the distinct field names in the “Name” fieldThe distinct() function is called to the “Name” field in this example, and it returns the names of separate fields in the “fosslinuxtuts” collection. To do this, we ran the following command in MongoDB Shell. db.fosslinuxtuts.distinct("Name") Display names field From the output above, it is evident that the “distinct()” method displays the names of the distinct fields specified in the command. Example 2: Extracting and Counting the number of distinct values in the “Name” fieldUsing the previous example, we will use the below command to count the number of unique fields in the “Name” fields of the “fosslinuxtuts” collection. db.fosslinuxtuts.distinct("Name").length Display name field count Example 3: Counting the number of distinct values in an array fieldThe “Designation” field in the “fosslinuxtuts” collection is an array containing the author’s designation and role. For instance, the command below will count the number of distinct values: db.fosslinuxtuts.distinct("Designation").length Count the number of distinct values Example 4: Using the distinct() method to query a conditionHere I will illustrate how to use the distinct() method to query a condition, and in such a circumstance, only the distinct values are returned, and they must match the query condition. For example, the following command will return the count of the distinct values present in the “Designation” field, and it must meet the query condition provided, which in this case is [Name: “WriterCode”] db.fosslinuxtuts.distinct("Designation", {Name: "WriterCode"}).length Query a condition From the above output, it is evident that there are “2” distinct fields within the “Designation” field in which the “Designation” matches the “Junior” provided. Example 5: Extracting and Counting the number of distinct values in a numeric fieldThe distinct approach is also applicable to MongoDB’s numerical data types. Similar to the “fosslinuxtuts” collection, the “WriterCode” field contains values that are of the “Integer” datatype. The command below counts how many different values there are in the “WriterCode” field. db.fosslinuxtuts.distinct("WriterCode").length Fields containing integer data types That’s it about using a distinct count query in your MongoDB ConclusionThis guide has comprehensively covered how to use the distinct query method in MongoDB. In addition, it has provided examples that will help you quickly grasp the concept of using the distinct query method. MongoDB, like any other database, plays a crucial role in the retrieval of documents, and it uses the distinct() method to retrieve the distinct values of a provided field. I hope you found this tutorial guide helpful, and if yes, feel free to give it a thumbs up. How to use count with distinct in MongoDB?To count the unique values, use "distinct()" rather than "find()", and "length" rather than "count()". The first argument for "distinct" is the field for which to aggregate distinct values, the second is the conditional statement that specifies which rows to select.
How to use group and count in MongoDB?We can use the following code to group by the 'position' field and count the occurrences of each position.. The position 'Forward' occurs 1 time.. The position 'Guard' occurs 3 times.. The position 'Center' occurs 1 time.. How do I get distinct records in MongoDB?To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.
What is count () in MongoDB?The count() method counts the number of documents that match the selection criteria. It returns the number of documents that match the selection criteria. It takes two arguments first one is the selection criteria and the other is optional. This method is equivalent to db. collection.
|