Python MongoDB – distinct()
Last Updated :
28 Jul, 2020
MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. It stores data in the form of key-value pairs and is a NoSQL database program. The term NoSQL means non-relational. Refer to MongoDB and Python for an in-depth introduction to the topic. Now let’s understand the use of distinct() function in PyMongo.
distinct()
PyMongo includes the distinct()
function that finds and returns the distinct values for a specified field across a single collection and returns the results in an array.
Syntax : distinct(key, filter = None, session = None, **kwargs)
Parameters :
- key : field name for which the distinct values need to be found.
- filter : (Optional) A query document that specifies the documents from which to retrieve the distinct values.
- session : (Optional) a ClientSession.
Let’s create a sample collection :
from pymongo import MongoClient
client = MongoClient()
database = client[ 'database' ]
mycollection = mydatabase[ 'myTable' ]
documents = [{ "_id" : 1 , "dept" : "A" ,
"item" : { "code" : "012" , "color" : "red" },
"sizes" : [ "S" , "L" ]},
{ "_id" : 2 , "dept" : "A" ,
"item" : { "code" : "012" , "color" : "blue" },
"sizes" : [ "M" , "S" ]},
{ "_id" : 3 , "dept" : "B" ,
"item" : { "code" : "101" , "color" : "blue" },
"sizes" : "L" },
{ "_id" : 4 , "dept" : "A" ,
"item" : { "code" : "679" , "color" : "black" },
"sizes" : [ "M" ]}]
mycollection.insert_many(documents)
for doc in mycollection.find({}):
print (doc)
|
Output :
{'_id': 1, 'dept': 'A', 'item': {'code': '012', 'color': 'red'}, 'sizes': ['S', 'L']}
{'_id': 2, 'dept': 'A', 'item': {'code': '012', 'color': 'blue'}, 'sizes': ['M', 'S']}
{'_id': 3, 'dept': 'B', 'item': {'code': '101', 'color': 'blue'}, 'sizes': 'L'}
{'_id': 4, 'dept': 'A', 'item': {'code': '679', 'color': 'black'}, 'sizes': ['M']}
Now we will; use the distinct()
method to :
- Return distinct values for a Field
- Return Distinct Values for an Embedded Field
- Return Distinct Values for an Array Field
- Return Specific Query
print (mycollection.distinct( 'dept' ))
print (mycollection.distinct( 'item.color' ))
print (mycollection.distinct( "sizes" ))
print (mycollection.distinct( "item.code" , { "dept" : "B" }))
|
Output :
['A', 'B']
['red', 'blue', 'black']
['L', 'S', 'M']
['101']
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...