Example 1: Display the Unique Field Values from Multiple Records
Before doing anything in the MongoDB shell, make sure that your database has its collection within it that also holds some records; collections work like tables in MongoDB. If you don’t have one, generate it within the MongoDB command-line tool using the simple “createCollection” method in its instruction that starts with the “db” keyword. The argument of a createCollection() method takes the name of a collection as a “Unique” string.
Without having any records in the database, we will be unable to perform the transactions through queries. Therefore, after the creation of an empty “Unique” collection in MongoDB, we insert some records in it. For the insertion of multiple records at a time, the “insert” function of MongoDB can be cast off with the name of the “Unique” collection. To insert multiple records, hold all the records in the square brackets “[]” while using the “{}” curly brackets to separate one from another within the square brackets. For now, we insert a total of 8 records, each having three fields – id, Name, and Age. These records are added perfectly since the output that is displayed in the attached photo demonstrates the acknowledgment. There are repeated “same” values for the “Name” field and “Age” field somewhere in these 8 records.
… {id: 3, Name: "Paul", Age: 44}, {id: 4, Name: "Bryan", Age: 40},
… {id: 5, Name: "Paul", Age: 40}, {id: 6, Name: "Paul", Age: 45},
… {id: 7, Name: "Bryan", Age: 20}, {id: 8, Name: "Peter", Age: 45} ])
You can find and display the newly inserted records from your database collection very easily by casting off the “find” function in the instruction. This function uses the “db” keyword followed by the “Unique” collection name and the “find” function with the empty brackets.
Now that we have a collection with many records in it, we can use the “Unique” collection in our illustration to discuss the use of a “distinct” query. Hence, the query starts with the same “db” keyword followed by the “Unique” collection name and ends with the “distinct” function. The distinct() function contains the name of a specific field of a collection in its argument to get and display the unique records which are not repeated even once. Let’s say we want to have a look at all the unique records from the “Name” field of the “Unique” collection. Therefore, we use the “Name” field in the argument of the “distinct” function within the following listed instruction. The output of this query only displays the names from the “Name” field which are unique. If a name is repeated many times, it only displays it once in the output like an intersection.
The very same step is repeated for the “Age” field of the “Unique” collection to get all the ages uniquely without any repetition. In return, we get four unique values in a row.
Example 2: Display the Unique Records from Nested Multiple Records
Now that we are done with the single-line records to get the unique values from the particular field of a collection, we are ready to get the unique values from the nested records of a “Unique” collection which is a document within a document field. For that, we update the “Unique” collection and add four new records, each containing the nested “Data” value within it. The “Data” field contains two fields within it and makes a nested document in MongoDB. The same “insert” function is used in the query with the “Unique” collection name. All these four records are added successfully. The acknowledgment output is demonstrated in the following image:
... { id: 10, Name: "Tim", Data: {Age: 40, Score: 55}},
... { id: 11, Name: "Tem", Data: {Age: 10, Score: 50}},
... { id: 12, Name: "Tom", Data: {Age: 40, Score: 55}} ])
After running the “find” function instruction of MongoDB, we get the items of a “Unique” collection on the shell screen in ascending order of the “id” field. Moving down in the list of 12 records, we find the recently added 4 records in this output as displayed in the following. Now that you have taken a look at the last 4 records of the Unique collection and have an experience in identifying the unique records through single-line records, you will learn about getting the unique records from the document-type fields.
Let’s say you want to get all the unique values from the “Score” field of a document-type “Data” field in a “Unique” collection. To use the “distinct” function to identify and display all the values once (not repeated at all), we have to use the name of a document-format “Data” field along with its internal field to be displayed which is the “Score”. Both these fields are connected through a “dot” to get the values of the internal field through its external field name. The overall syntax of a query is the same – the “db” keyword followed by a “Unique” collection name and the “distinct” function with its arguments. The output for the execution of this query displays only two values from the rest of the four by only picking 1 value from the duplicate records.
The very same thing happens for the “Age” field that resides inside the document-type “Data” field. The following output shows only two records from the rest of the four records:
Conclusion
By comparing our real-life scenarios with the programming field, we experienced the simplest and easiest journey to get a knowledge about the “distinct” function of MongoDB in this article. The guide’s introductory paragraph is all about that comparison and the use of the distinct() function in the MongoDB environment. Through the demonstrated illustrations, we now know how to use the distinct function to get the values only once at a shell.