Python MongoDB समूह बनाएँ
- पिछला पृष्ठ MongoDB डाटाबेस सिर्जना
- अगला पृष्ठ MongoDB इंसर्ट
MongoDB में समूह SQL डाटाबेस में तालिकाओं के समान हैं。
समूह बनाएँ
MongoDB में समूह बनाने के लिए डाटाबेस ऑब्जैक्ट का उपयोग करें और बनाने के लिए समूह का नाम निर्दिष्ट करें。
यदि यह मौजूद नहीं है, MongoDB इस समूह को बनाएगा。
Example
"customers" नाम का समूह बनाएँ:
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"]
Important Note:In MongoDB, collections are not created before getting content!
MongoDB will wait until you have inserted documents before creating a collection in reality.
Check if the collection exists
Remember: In MongoDB, collections are not created before getting content, so if this is your first time creating a collection, you should complete the next chapter (create documents) before checking if the collection exists!
You can check if a collection exists in the database by listing all collections:
Example
Return a list of all collections in the database:
print(mydb.list_collection_names())
Or you can check a specific collection by name:
Example
Check if the "customers" collection exists:
collist = mydb.list_collection_names() if "customers" in collist: print("The collection exists.")
- पिछला पृष्ठ MongoDB डाटाबेस सिर्जना
- अगला पृष्ठ MongoDB इंसर्ट