Updated February 28, 2023

## Introduction to MongoDB Distinct

MongoDB distinct is used to find distinct values from a single collection and retrieved results of the query in an array, MongoDB distinct is used to retrieve distinct values from the collection. Distinct in MongoDB is very important and useful to retrieve distinct data from a single collection. Distinct is different from MongoDB find the method, distinct will returns the array of all the distinct values from the collection. This will return only distinct data from the collection, if in one collection thousands of documents and we need only those documents which were distinct each other same time we have used distinct in MongoDB.

### Syntax

Below is the syntax of distinct in MongoDB.

`collection_name.distinct (field, query)`

`collection_name.distinct (field, query, options)`

#### Parameter

Below is the parameter description:

**Collection name:**Collection name is defined as retrieved documents from the collection by using a distinct method. We can retrieve distinct values from the collection by using distinct methods.**Distinct:**This will return only distinct data from the collection. This is very important and useful to retrieve distinct data from a single collection. This is used to retrieve distinct values from the collection.**Field:**Field has specified that return distinct value from the field we have specified with distinct methods. The field is an essential and useful parameter in a distinct method.**Options:**This is an optional parameter in MongoDB distinct command. This is an additional option used in the distinct command. It is available only if the document specifies the options.**Query:**Query distinct method defines the document from which we have to retrieve the collection’s distinct value. The query is a handy and important parameter in a distinct method.

### How Distinct Command works in MongoDB?

Below is the working of distinct command:

- The distinct method in MongoDB uses an index on the field while doing a distinct operation on collection.
- This will return only distinct data from the collection, if in one collection thousands of documents and we need only those documents which were distinct each other same time we have used distinct in MongoDB.
- When we have used or specifies collation in a distinct method, we need to specify a mandatory locale to define the field with collation in a distinct method.
- If the field’s values were an array, then distinct methods consider each element of the field as a separate value.
- If suppose array contains a value as [1, 2, 1, [1], 2], then distinct methods consider it as [1, 2, [1]] as a result.

Below example states will consider the array field as each element as separate values.

**Code #1**

`use stud_array`

**Output:**

**Code #2**

`db.stud_array.insert({"stud_id" : 1, "semester" : 5, "grades" : [ 90, 90, [92], 92 ] })`

**Output:**

**Code #3**

`db.stud_array.distinct("grades")`

**Output:**

### Examples to Implement MongoDB Distinct

Below is the example of a distinct method in MongoDB.

We have taken an example of student_datatable to describe an example of a distinct method is as follows. Below is the data description of the student_data table are as follows.

#### Example #1

`use student_data`

#### Example #2

`db.student_data.find()`

##### 1. Return distinct values from field

The below example shows that return distinct value from the field. We have used the semester field from the student_data table to describe distinct values from the field.

**Code:**

`db.student_data.distinct ("semester")`

** Output:**

**Explanation:** In the above example, we have retrieved distinct values from the semester field. The result of values from the customer field is 3, 4, and 5.

##### 2. Return distinct values from field

The below example shows that return distinct value from the array field. We have used the grade array field from the student_data table to describe distinct values from the array field.

**Code:**

`db.student_data.distinct ("grades")`

**Output:**

**Explanation:** In the above example, we have retrieved distinct values from the grades field. The above example states that distinct methods in MongoDB consider the array field as each element of the field as a separate value. In the above example output of 92 and 95 occur double because distinct considered it a separate element.

##### 3. Query with a distinct method

The below example shows that query with the distinct value from the field. We have used semester and grades field from the student_data table to describe queries with distinct fields.

**Code:**

`db.student_data.distinct ("semester",{ grades: 90 })`

**Output:**

##### 4. Using collation with a distinct method

The below example shows that collation with the distinct method in MongoDB. We have used the semester field from the student_data table to describe collation with the method.

**Code:**

`db.student_data.distinct( "semester", {}, { collation: { locale: "fr", strength: 1 } } )`

**Output:**

### Recommended Articles

This is a guide to MongoDB Distinct. Here we discuss syntax, parameter, the working of distinct command with examples to implement. You can also go through our other related articles to learn more –