توصیه‌های دوره:

مجموعه‌های MongoDB با جدول‌های بانک اطلاعاتی SQL مشابه هستند.

ایجاد مجموعه

برای ایجاد مجموعه در MongoDB، از اشیای پایگاه داده استفاده کنید و نام مجموعه‌ای که می‌خواهید ایجاد کنید را مشخص کنید.

اگر آن وجود نداشت، MongoDB آن مجموعه را ایجاد خواهد کرد.

Example

تاسیس مجموعه‌ای به نام "customers":

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

Run Instance

Important Tip:Collections in MongoDB are not created before obtaining content!

Before actually creating a collection, MongoDB will wait until you have inserted a document.

Check if the collection exists

Remember: In MongoDB, collections are not created before content is obtained, so if this is your first time creating a collection, you should complete the next chapter (create document) 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())

Run Instance

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.")

Run Instance