VOOZH about

URL: https://www.geeksforgeeks.org/python/python-mongodb-distinct/

⇱ Python MongoDB - distinct() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python MongoDB - distinct()

Last Updated : 4 Jul, 2025

distinct() method in MongoDB is used to retrieve all unique values for a specified field from a collection. In Python, you can use this method with PyMongo to filter out duplicates and get a list of distinct values across documents. This is helpful when you want to find all unique fields without repeating values.

Syntax

collection.distinct(key, filter=None)

Parameters :

  • key (string): field name for which you want to find distinct values.
  • filter (optional, dict): a query to filter which documents to consider when finding distinct values.

Let's create a sample collection:

Output

👁 distinct-database
Snapshot of Terminal showing Collection created

Example:

In this Example, distinct() method is used to return unique values of different fields from the collection.

Output

👁 distinct-output
Snapshot of Terminal showing Output of Distinct method

Related Article:

Comment
Article Tags: