EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Distinct
 

MongoDB Distinct

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB-Distinct

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

array field

Code #2

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

Output:

array field

Code #3

db.stud_array.distinct("grades")

Output:

MongoDB Distinct - 3

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

MongoDB Distinct - 4

Example #2

db.student_data.find()

MongoDB Distinct - 5

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:

Return distinct

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:

Return distinct

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:

Query

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:

Using collation

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 –

  1. Indexes in MongoDB
  2. How To Install MongoDB
  3. MongoDB Alternatives
  4. MongoDB Aggregate
  5. MongoDB Versions | How to Check?
  6. MongoDB Unique | How to Work?
  7. MongoDB insert | How to Work?
  8. How to Work MongoDB Update?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW