How to access a collection in MongoDB using Python?

MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. MongoDB offers high speed, high availability, and high scalability.

Accessing a Collection

1) Getting a list of collection: For getting a list of a MongoDB database’s collections list_collection_names() method is used. This method returns a list of collections.

Syntax:

list_collection_names()

 
Example:
 

Sample Database:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
  
# create an client instance of the 
# MongoDB class
mo_c = MongoClient()
  
# create an instance of 'some_database'
db = mo_c.GFG
  
# get a list of a MongoDB database's 
# collections
collections = db.list_collection_names()
print ("collections:", collections, "\n")

chevron_right


Output:

collections: ['Geeks'] 

2) Check if the collection exist or not: To check if the collection attribute exists for the database use hasattr() method. It returns true if the collection is in database otherwise returns false. 

Syntax: hasattr(db, ‘collectionname’)

Parameters:
db: It is database object. 
collectionname: It is the name of the collection. 

Example:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
  
# create an client instance of 
# the MongoDB class
mo_c = MongoClient()
  
# create an instance of 'some_database'
db = mo_c.GFG
  
# check collection is exists or not 
print(hasattr(db, 'Geeks'))

chevron_right



Output:

True

3) Accessing a Collection: To acces a MongoDB collection name use the below syntax. 
 

Syntax:

database_object.Collectionname
or
database_object["Collectionname"]

Note: Database_object[“Collectioname”] can be useful in the case where the name of the collection contains a space in between them i.e. in cases like database_object[“Collection name”].

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
  
# create an client instance of
# the MongoDB class
mo_c = MongoClient()
  
# create an instance of 'some_database'
db = mo_c.GFG
  
col1 = db["gfg"]
  
print ("Collection:", col1)

chevron_right


 
Output: 

Collection: Collection(Database(MongoClient(host=[‘localhost:27017’], document_class=dict, tz_aware=False, connect=True), ‘GFG’), ‘gfg’)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.