Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Get the Names of all Collections using PyMongo
Next article icon

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
frompymongoimportMongoClient# create an client instance of the# MongoDB classmo_c=MongoClient()# create an instance of 'some_database'db=mo_c.GFG# get a list of a MongoDB database's# collectionscollections=db.list_collection_names()print("collections:",collections,"\n")

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
frompymongoimportMongoClient# create an client instance of# the MongoDB classmo_c=MongoClient()# create an instance of 'some_database'db=mo_c.GFG# check collection is exists or notprint(hasattr(db,'Geeks'))


Output:

True

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

Syntax:

database_object.Collectionnameordatabase_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
frompymongoimportMongoClient# create an client instance of# the MongoDB classmo_c=MongoClient()# create an instance of 'some_database'db=mo_c.GFGcol1=db["gfg"]print("Collection:",col1)

 
Output: 

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


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp