

The following example will help you to understand the problem more precisely: It eventually helps us return only unique values for a field we specify in the mongo shell. The distinct () function returns an array of unique values for a specified field across a single collection. The $group stage is used by the aggregation process to get the separate item values from the collection. Sometimes you want to return only unique values in MongoDB which can be achieved by using the distinct () function. In this topic, you will learn to find distinct fields using aggregation operations.
MONGODB COMPASS QUERY DISTINCT VALUES HOW TO
Read: How to create the indexes in MongoDB? Distinct query in aggregate MongoDB Note that, The benefits of indexes, when we have a large dataset and we want to find out distinct fields on the time with the help of indexes we can easily retrieve the distinct documents fields. We have successfully retrieved the distinct fields using an index. The find() method will use to return the documents of the collection.ĭistinct fields in MongoDB using an index The following documents were inserted into the inventory collection. Now, You will more understand with the help of an example. So, In this topic, you will learn to apply the distinct query with conditions. Sometimes we have to apply the condition using the distinct method to find a particular document. We have successfully retrieved the distinct value from the collection.Īlso, check: MongoDB Auto Increment ID Distinct query in MongoDB with condition Return all the fields of the fname column.db.students.insertMany([Īfter that, we will apply the below query to return the distinct name (fname) of all the students present in the collection: db.student.distinct("fname") The following documents were inserted into the students’ collection.

You will more understand this with the help of an example. In this topic, you will learn to find the distinct values for a given field across a single collection and return the result in an array. One more awesome feature is we can do pagination very easily using the query object, if we see i have added the Pageable variable while creating the query object and while querying the database it. Note, you will the aggregation pipeline to retrieve distinct values using the $group operator. Optional, A document that specifies the options. just curious to learn, how to achieve the same with compass tool as I will be using it frequently. For an example, see Return Distinct Values for an Array Field. For instance, if a field has as its value 1, 1, 1, then db.collection.distinct () considers 1, 1, and 1 as separate values. I know how to create the query in mongo shell to select distinct fields. If the value of the specified field is an array, db.collection.distinct () considers each element of the array as a separate value. The field name for which you want to return distinct values.Ī query that specifies the documents from which to retrieve the distinct values. I installed only MongoDB Compass and connecting to MongoDB Atlas cluster. The command contains the following fields: parameter Syntax: db.collection.distinct(field, query, options) The results document also contains an embedded document with query statistics and the query plan. The distinct method will return a document that contains an array of the distinct values. MongoDB finds the distinct values for a defined field over a single collection.
